Repository模式--采用EF Fluent API使用EntityTypeConfiguration分文件配置Model映射关系
EF中类EntityTypeConfiguration是一个很有用的类,在nopCommerence中就使用这个类来分文件分文件配置Model映射关系。今天我就来谈谈Repository模式在Entity Framework Code First中使用EntityTypeConfiguration的实现例子。
背景
为了简化我们就只使用两个表:分类表Category,产品类Product。最终项目结构如下:
注意:EfRepPatTest.Entity和EfRepPatTest.Data是类库项目,EfRepPatTest.Implementation是控制台项目。项目EfRepPatTest.Data需要对Entity Framework类库的引用。
BaseEntity.cs
创建一个所有实体的基类BaseEntity,把一些公共的属性封装在里面。
- public class BaseEntity<T>
- {
- public T Id { get; set; }
- }
这样表示所有实体都有一个字段为Id,类型为泛型这样可以满足所有类型的情况。
IRepository.cs:
下面定义一个泛型的接口IRepository,其中包括一个泛型的增、删、改。
- public interface IRepository<TEntity> where TEntity:class
- {
- IQueryable<TEntity> GetAll();
- TEntity GetById(object id);
- void Insert(TEntity entity);
- void Update(TEntity entity);
- void Delete(TEntity entity);
- }
Category.cs:
实体分类类
- public class Category:BaseEntity<int>
- {
- public virtual string Name { get; set; }
- public List<Product> Products { get; set; }
- }
Product.cs:
实体产品类
- public class Product:BaseEntity<long>
- {
- public virtual int CategoryId { get; set; }
- public virtual Category Category { get; set; }
- public virtual string Name { get; set; }
- public virtual int MinimumStockLevel { get; set; }
- }
IDbContext.cs:
接口IDbContext封装一些EF的公共接口方法。
- public interface IDbContext
- {
- IDbSet<TEntity> Set<TEntity>() where TEntity:class;
- int SaveChanges();
- void Dispose();
- }
DataContext.cs:
- public class DataContext: DbContext,IDbContext
- {
- public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
- {
- return base.Set<TEntity>();
- }
- }
CategoryMap.cs:
分类映射类继承于EntityTypeConfigureation<T>
- public class CategoryMap:EntityTypeConfiguration<Category>
- {
- public CategoryMap()
- {
- ToTable("Category");
- HasKey(c => c.Id).Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
- Property(c => c.Name).IsRequired().HasMaxLength(50);
- }
- }
ProductMap.cs:
产品类映射类继承于EntityTypeConfigureation<T>
- public class ProductMap:EntityTypeConfiguration<Product>
- {
- public ProductMap()
- {
- ToTable("Product");
- HasKey(p => p.Id).Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
- //CategoryId as foreign key
- HasRequired(p => p.Category)
- .WithMany(c=>c.Products)
- .HasForeignKey(p => p.CategoryId);
- Property(p => p.Name).IsRequired().HasMaxLength(100);
- Property(p => p.MinimumStockLevel);
- }
- }
在类DataContext中重写OnModelCreating方法依次加上我们新建的EF的Map配置文件,加入以下代码:
- modelBuilder.Configurations.Add(new CategoryMap());
- modelBuilder.Configurations.Add(new ProductMap());
- base.OnModelCreating(modelBuilder);
上面的代码可以优化一下,可以利用反射自动添加EF的Map配置文件,如下:
- public class DataContext: DbContext,IDbContext
- {
- public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
- {
- return base.Set<TEntity>();
- }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
- .Where(type => !String.IsNullOrEmpty(type.Namespace))
- .Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
- type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
- foreach (var type in typesToRegister)
- {
- dynamicconfigurationInstance = Activator.CreateInstance(type);
- modelBuilder.Configurations.Add(configurationInstance);
- }
- base.OnModelCreating(modelBuilder);
- }
- }
这样的好处是以后新加EF的实体Map类,不用修改DataContext。
RepositoryService.cs:
IRepositroy接口的一个具体实现的RepositoryService,数据访问采用EF的IDbContext。
- public class RepositoryService<TEntity>:IRepository<TEntity> where TEntity:class
- {
- private IDbContext Context;
- private IDbSet<TEntity> Entities
- {
- get { return this.Context.Set<TEntity>(); }
- }
- public RepositoryService(IDbContext context)
- {
- this.Context = context;
- }
- public IQueryable<TEntity> GetAll()
- {
- return Entities.AsQueryable();
- }
- public TEntity GetById(object id)
- {
- return Entities.Find(id);
- }
- public void Insert(TEntity entity)
- {
- Entities.Add(entity);
- }
- public void Update(TEntity entity)
- {
- if (entity == null)
- throw new ArgumentNullException("entity");
- this.Context.SaveChanges();
- }
- public void Delete(TEntity entity)
- {
- Entities.Remove(entity);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (this.Context != null)
- {
- this.Context.Dispose();
- this.Context = null;
- }
- }
- }
- }
新建一个类DataBaseInitializer为EF Code First数据库访问的初始化类。
- public class DataBaseInitializer : IDatabaseInitializer<DataContext>
- {
- public void InitializeDatabase(DataContext context)
- {
- context.Database.CreateIfNotExists();
- }
- }
新建一个控制台程序来测试上面的代码Program.cs:
- class Program
- {
- static void Main(string[] args)
- {
- var context = new DataContext();
- var dataBaseInitializer = new DataBaseInitializer();
- dataBaseInitializer.InitializeDatabase(context);
- var categoryRepository = new RepositoryService<Category>(context);
- //Adding category in the category entity
- var category = new Category()
- {
- Name = "Baverage"
- };
- var products = new List<Product>();
- //Adding product in the product entity
- var product = new Product()
- {
- Name = "Soft Drink A",
- MinimumStockLevel = 50
- };
- products.Add(product);
- product = new Product()
- {
- Name = "Soft Drink B",
- MinimumStockLevel = 30
- };
- products.Add(product);
- category.Products = products;
- //Insert category and save changes
- categoryRepository.Insert(category);
- context.SaveChanges();
- ///
- /For the next project we shall add Dependency Injection
- But now we have add a Service layer for test manually//
- ///
- IProductService productRepository = new ProductService();
- Console.WriteLine("\n");
- Console.WriteLine("Product List:");
- Console.WriteLine("-------------------------------------------------");
- foreach (var product1 in productRepository.GetAll())
- {
- Console.WriteLine(string.Format("Product Name : {0}",product1.Name));
- if (product1.Id == 9)
- {
- product1.Name = "Soft Drink AAA";
- productRepository.Update(product1);
- }
- }
- Console.WriteLine("Press any key to exit");
- Console.ReadKey();
- }
- }
在配置文件添加数据库的链接。
App.config:
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <configSections>
- <!-- For more information
- on Entity Framework configuration, visit
- http://go.microsoft.com/fwlink/?LinkID=237468 -->
- <section name="entityFramework"
- type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
- EntityFramework, Version=4.4.0.0, Culture=neutral,
- PublicKeyToken=b77a5c561934e089"
- requirePermission="false" />
- </configSections>
- <entityFramework>
- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,
- EntityFramework" />
- </entityFramework>
- <connectionStrings>
- <add name="DataContext"
- providerName="System.Data.SqlClient"
- connectionString="Data
- Source=YourSERVER;Initial Catalog=EfDBExistRepository;Integrated
- Security=True;MultipleActiveResultSets=True;"/>
- </connectionStrings>
- </configuration>
注意:数据库链接结点名为“DataContext”,正好和我们自己写的类“DataContext”名字一样。这样EF框架就可以自动找到这个数据库链接信息。
参考:http://www.codeproject.com/Articles/561584/Repository-Pattern-with-Entity-Framework-using
转载于:https://www.cnblogs.com/Jeely/p/10953955.html
Repository模式--采用EF Fluent API使用EntityTypeConfiguration分文件配置Model映射关系相关推荐
- java EF6,EF Core 2.0和EF6(Entity Framework 6)中配置实体映射关系
1.EF6中通过EntityTypeConfiguration配置实体映射关系代码 public class AccountMap : EntityTypeConfiguration { public ...
- ef core中如何实现多对多的表映射关系
文档:https://docs.microsoft.com/en-us/ef/core/modeling/relationships class MyContext : DbContext{publi ...
- 深入了解EntityFramework——Fluent API
Fluent API 除了惯例原则与属性数据注解外,FluentAPI是另一种支持实体类配置设置的方式.与属性数据注解相比,它提供了更广泛的功能与设置弹性.实体类若同时设置了数据注解,则采用的优先权是 ...
- Fluent API
访问Fluent API 通常通过重写派生的 DbContext 上的 OnModelCreating 方法来访问 Code First Fluent API public class SchoolC ...
- 第六节:框架搭建之EF的Fluent Api模式的使用流程
一. 前言 沉寂了约一个月的时间,今天用一篇简单的文章重新回归博客,主要来探讨一下Fluent Api模式在实际项目中的使用流程. 1. Fluent API属于EF CodeFirst模式的一种,E ...
- .net ef 字段不区分大小写_第六节:框架搭建之EF的Fluent Api模式的使用流程
一. 前言 沉寂了约一个月的时间,今天用一篇简单的文章重新回归博客,主要来探讨一下Fluent Api模式在实际项目中的使用流程. 1. Fluent API属于EF CodeFirst模式的一种,E ...
- 第十六节: EF的CodeFirst模式通过Fluent API修改默认协定
一. 简介 1. 优先级:Fluent API > data annotations > default conventions. 2. 所有的Fluent API配置都要在 OnMode ...
- EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
为什么80%的码农都做不了架构师?>>> EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射 I.EF里的默认映射 上篇文章演示 ...
- EF CodeFirst 学习 1 - 用fluent API设置元数据,
用 Fluent API 设置元数据 http://agilenet.wordpress.com/2011/04/11/entity-framework-4-1-rc-with-an-existing ...
- EF使用Fluent API配置映射关系
定义一个继承自EntityTypeConfiguration<>泛型类的类来定义domain中每个类的数据库配置,在这个自定义类的构造函数中使用我们上次提到的那些方法配置数据库的映射. 映 ...
最新文章
- TCP为什么是3次握手?
- 5G NPN 行业专网 — Overview
- php正则截取富文本编辑器中路径字符串_php使用正则表达式获取字符串中的URL
- div中的内容水平垂直居中
- HDU 2191 - 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (多重背包)
- Java面试必问!Spring事务扩展机制(2)
- c语言程序设计笔记手写图片,C语言程序设计笔记.pdf
- 远程连接mysql速度慢的解决方法:skip-name-resolve取消DNS的反向解析
- Kettle构建Hadoop ETL实践(五):数据抽取
- 10分钟了解Activity工作流
- 使用DBUtils报错connot create bean 错误解决的办法
- 台式计算机调亮度快捷键,台式电脑怎么调节屏幕亮度
- Unity手机游戏广告接入的大致思路(Android和iOS)
- xpath定位元素详解
- 学计算机颈椎,长期玩电脑颈椎病
- Windows下安装Nexus私服及更新索引
- 2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(10)
- linux 字符界面
- 2022-2028年全球与中国紫外线(UV)传感器行业竞争格局与投资战略研究
- 公众号文章写作平台有哪些