积极答复者
Entity Framework4.1 数据库优先模式,如何映射已存在的实体类

问题
-
需要对一个项目进行升级改造.希望采用Entity Framework4.1编写新增加的代码.原项目采用N层模式开发,项目架构图如下(新增了部分用于MVC的类库,可以忽略):
现在希望在SQLServerDAL层中使用EF.但是希望EF的model对应到原程序的EasyFastCMS.Model层中(希望Model层独立).我发现Code First模式可以这么实现,例如:
namespace EasyFastCMS.WebMvc.DAL { public class EasyFastCMSContext : DbContext { private readonly static string CONNECTION_STRING = "name=DataBase"; public EasyFastCMSContext() : base(CONNECTION_STRING) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); modelBuilder.Entity<UserGroup>().ToTable("EasyFastCMS_UserGroup"); } public DbSet<UserGroup> UserGroup { get; set; } } }
(以上代码为在MVC中做尝试,实现了EF的model对应MVC中的M)
但是,数据库已存在,并且有大量有用数据,并且我不想使用Code First模式,而希望使用 数据库优先模式.请问在DataBase First模式中,能不能做到类似Code First模式的把 Model独立出来?
答案
-
你可以看看这个link: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx
应该能解决你的问题。
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Alan_chenModerator 2011年12月27日 6:05
全部回复
-
你可以看看这个link: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx
应该能解决你的问题。
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Alan_chenModerator 2011年12月27日 6:05