Entity Framework 4.3 CodeFirst 如何将model映射到不同的数据库中?

cnuusw 2012-06-06 10:13:55

public class SiteDbContext:DbContext
{
public SiteDbContext() : base("DataBase") { }
public SiteDbContext(string conn){ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//modelBuilder.Ignore<User>();
base.OnModelCreating(modelBuilder);
}
public DbSet<User> User { get; set; }
public DbSet<UserType> UserType { get; set; }
public DbSet<UserExtend> UserExtend { get; set; }
public DbSet<Office> Office { get; set; }
public DbSet<Office_Log> Office_Log { get; set; }
public DbSet<BussinessDepartment> BussinessDepartment { get; set; }
}



如上代码所示,在一个多数据库应用中,通过多构造函数,实现SiteCo0ntent 对应不同的数据库,现在发现在两个数据库中创建一样的表。
希望将User表公用,放在单独的数据库中,请问如何实现,在OnModelCreating方法中写代码吗?求一个示例。
...全文
2971 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
寒飞a 2015-01-16
  • 打赏
  • 举报
回复
楼主不知道是否有解决,我现在也遇到同样的问题,而且是不同数据库、然后有N个数据库的结构。 程序有2个结构不通的库 1、程序管理库,比如有 corp等表。 2、程序业务库,全部业务平台的 表等。 现在要把model映射到这2个不同数据库,程序管理器有1个,而程序业务库,是以公司为单位,有N个。 现在我也急需实现2个东西,1:把不同的model映射到不同的数据库中, 我的解决办法: 首先我用程序集的方式将所有类型为model的基类的保存在一个集合中,并且提供了一个委托,返回基类(model)的集合 然后在DbContext 重写OnModelCreating时,首先我会给DbContext一个构造函数,里面传入了连接字符串(应对多个业务库的变化), 然后OnModelCreating重写时我通过静态保存的委托去读取集合中应该属于当前连接字符串规则的model集合,添加到 modelBuilder.Configurations.Add().. 这样从理论上来讲,是能通过的,但是我发现我切换字符串的问题解决了(可以来回与多个业务库之间操作)。但是管理库的结构和业务库的结构还是一样的,并没有按照我设想的变化。
风骑士之怒 2013-01-06
  • 打赏
  • 举报
回复
两个context
YiYanXiYin 2012-12-19
  • 打赏
  • 举报
回复
将User放在单独的dbcontext文件中
q107770540 2012-07-12
  • 打赏
  • 举报
回复
数据库表结构和映射实体最好要保持一一对应
Microsoft’s principal data access technology ADO NET Entity Framework has had two major releases as part of the NET Framework NET 3 5 brought us the first version of Entity Framework which is covered in the first edition of Programming Entity Framework In 2010 Microsoft NET 4 was released; it contained the next version of Entity Framework referred to as Entity Framework 4 The completely revised second edition of Programming Entity Framework was dedicated to teaching readers how to use this version of Entity Framework in Visual Studio 2010 When NET 4 was released the Entity Framework team was already hard at work on a new addition called Code First to provide an alternative to building the Entity Data Model that is core to Entity Framework Rather than using a visual designer Code First allows you to create the model from your existing classes This book is dedicated to teaching readers how to use Code First to build and configure a model based on the classes in your business domain While Code First can do much of the job of inferring a model from your classes there is quite a lot that you can do to affect the model that Code First creates In this book you will learn what Code First does by default aka convention and how to perform further configuration to affect how it understands your properties classes relationships and the database schema they map to whether you use Code First to help create a database or you want to use it with an existing database With this knowledge you can reap the benefits of the Entity Framework while leveraging existing classes or those classes you might be building for a new software project ">Microsoft’s principal data access technology ADO NET Entity Framework has had two major releases as part of the NET Framework NET 3 5 brought us the first version of Entity Framework which is covered in the first edition of Programming Entity Framework In 2010 Microsoft NET 4 was released; [更多]

8,494

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧