如何使用实体框架

时间:2015-11-11 12:41:02

标签: c# asp.net-mvc entity-framework

我正在使用C#,。net 4.5,ASP.NET MVC 5和Entity Framework 6重做旧应用程序。

由于此Web应用程序将与旧应用程序同时使用,因此需要共享相同的数据库。

为了节省我一些时间,我已根据现有数据库生成了我的模型。 (要做到这一点,我首先收集同一数据库中的所有表,定义它们的关系,根据每个表的新数据库生成模型,然后使用旧数据库运行我的代码)

enter image description here

为了澄清我的问题,我创建了这个简单的例子。

一个人可以属于许多群体。

一个表Persons在DB1中,表Groups在DB2中。

这两个表之间的关系不存在,因为它们位于不同的数据库中。但我应该能够知道一个人属于哪个群体,以及哪个群体属于特定群体。

为类人员生成的代码是:

public partial class Persons
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Person Name")]
    public string Name { get; set; }

    public int GroupID { get; set; }

    public virtual Groups Groups { get; set; }
}

和Groups类:

public partial class Groups
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Groups()
    {
        Persons = new HashSet<Persons>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Group Name")]
    public string Name { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Persons> Persons { get; set; }
}

然后我为每个数据库创建了一个上下文

<add name="ModelContext1" connectionString="data source=PROG-PC\SQLEXPRESS;initial catalog=DB1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
<add name="ModelContext2" connectionString="data source=PROG-PC\SQLEXPRESS;initial catalog=DB2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

和各自的上下文类(我不确定我是否应该保留与外键相关的代码,因为它们实际上并不存在)

public partial class ModelContext1 : DbContext
    {
        public ModelContext1()
            : base("name=ModelContext1")
        {
        }

    public virtual DbSet<Persons> Persons { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Groups>()
            .HasMany(e => e.Persons)
            .WithRequired(e => e.Groups)
            .HasForeignKey(e => e.GroupID)
            .WillCascadeOnDelete(false);
    }
}



public partial class ModelContext2 : DbContext
    {
        public ModelContext2()
            : base("name=ModelContext2")
        {
        }

    public virtual DbSet<Groups> Groups { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Groups>()
            .HasMany(e => e.Persons)
            .WithRequired(e => e.Groups)
            .HasForeignKey(e => e.GroupID)
            .WillCascadeOnDelete(false);
    }
}

我的第一个问题是我的PersonControler:

public class PersonsController : Controller
    {
    private ModelContext1 db = new ModelContext1();
    private ModelContext2 db2 = new ModelContext2();

    // GET: Persons
    public ActionResult Index()
    {
        var persons = db.Persons.Include(p => p.Groups);
        return View(persons.ToList());
    }

enter image description here

(请记住我是MVC和EntityFramework的新手)

我该如何解决这个问题?

我应该考虑哪些其他因素?

1 个答案:

答案 0 :(得分:0)

我意识到这是一年之后,但是因为我偶然发现它可能会帮助处于类似情况的其他人。我们有一个用户/客户端数据库,用于存储用户和客户端登录,角色等的身份数据......然后我们的应用程序数据库存储实际的应用程序数据。我们为客户提供物理分区数据的能力,作为系统重写的一部分,我们正在寻找各种方法来保持我们的理智,随着客户群的增长。我们决定使用EF 6并且需要一种方法将用户与应用程序中的对象相关联,我们正在尝试我找到的解决方案here。计划是创建公共同义词(在上面的示例中为db2)并使用它在Context Configuration中创建关系。对该答案的评论似乎是积极的,我们仍然在努力,但我们会在我们启动并运行后更新此答案。