gpt4 book ai didi

asp.net-mvc - 多个 DbContexts 不同的数据库

转载 作者:行者123 更新时间:2023-12-01 14:04:47 24 4
gpt4 key购买 nike

我是 EF Code First 的新手,几周来一直很喜欢它。我面临着处理 2 个数据库(都在同一个 Sqlserver 中)。第一个数据库包含一个表,我只需要引用它的主键,它与我创建的表“逻辑上”相关(第二个 DbContext -- 第二个数据库)。基于这个键,我将查询我的第二个 DbContext 表(mvc 站点的实际相关表)。顺便说一下,Customer 有 0..* 个 CustomerUsers。

第一个数据库上下文:

public class Customer
{

[StringLength(10, MinimumLength = 3), Required]
public string CompanyID { get; set; }

[StringLength(8, MinimumLength = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string CustomerID { get; set; }


[StringLength(500)]
public string CustomerName { get; set; }


//*Initially, under the same dbContext, they were related, but now am separating
//them because of physical database separation; hence the navigation property
//public virtual ICollection<CustomerUser> CustomerUsers { get; set; }

}

在 OnModelCreating 方法中:

modelBuilder.Entity<Customer>()
.HasKey(c => new { c.CompanyID, c.CustomerID});

“逻辑上”相关的实体

public class CustomerUser
{

[Display(Name = "Customer ID")]
public int CustomerUserID { get; set; }

//[ForeignKey("Customer")]
[Display(Name = "CompanyID"), Required]
[StringLength(10, MinimumLength = 3)]
public string CompanyID { get; set; }

//[ForeignKey("Customer")]
[Display(Name = "CustomerID"), Required]
[StringLength(8, MinimumLength = 3)]
public string CustomerID { get; set; }


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

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

//*Initially related thru foreign key to Customer but separation of database
//thus no more relations – just a querying of Customer’s id from other database
//public virtual Customer Customer { get; set; }


}

OnModelCreating方法:

modelBuilder.Entity<CustomerUser>()
.HasKey(c => new { c.CustomerUserID });

modelBuilder.Entity<CustomerUser>()
.HasRequired(p => p.Customer)
.WithMany(c => c.CustomerUsers)
.HasForeignKey(p => new {p.CompanyID, p.CustomerID});

如何以及在何处声明我的 DbContext?目前我有一个由网络应用程序引用的程序集。另外,这对我的部署有何影响?仅用于引用其键的那个表已经存在,而其余表,我还没有在 SqlServer 中创建(它们存在于我的 VS2012 Server Explorer 中)。

使用此设计我会遇到任何与部署相关的问题吗?

最佳答案

How and where do i declare my DbContexts?

大问题。数据访问层是典型的。不在核心/领域层。这将核心与 EF 耦合。

Currently I have an assembly that is referenced by the web app. Also, how would this pan out for my deployment?

如果您不使用 CORE/DOMAIN 层 + 数据访问层 + UI 层,开发将与数据访问工具紧密耦合。

That one table used for just referencing its key is already existing, while the rest of the tables, I will have yet to create in SqlServer(they're existing in my VS2012 Server Explorer). Would there be any deployment-related issues I could run into with this design?

不,来自一个解决方案的多上下文多数据库访问对于 EF 来说不是问题。尽管实例化上下文的方式需要谨慎管理。例如https://stackoverflow.com/a/20118259/1347784

关于asp.net-mvc - 多个 DbContexts 不同的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20277940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com