[英]How to create a foreign relation to one Model from multiple other models using Entity framework?
I am trying to create my first app using ASP.NET MVC framework and Entity Framework 6. 我正在尝试使用ASP.NET MVC框架和Entity Framework 6创建我的第一个应用程序。
I chose to use code first approach and I started by defining my Models. 我选择使用代码优先方法,我开始定义我的模型。
I have a model called Client
with an identity attribute called Id
. 我有一个名为Client
的模型,其标识属性称为Id
。 I have multiple Models that has an attribute called ClientId
. 我有多个模型,它们有一个名为ClientId
的属性。 The ClientId
attribute should have virtual link to the Clients
Model. ClientId
属性应具有到Clients
模型的虚拟链接。
Here is how my Client
model looks like 这是我的Client
模型的样子
[Table("clients")]
public class Client
{
[Key]
public int id { get; set; }
public string name { get; set; }
public string status { get; set; }
public DateTime created_at { get; set; }
public DateTime? modified_at { get; set; }
public Client()
{
status = "Active";
created_at = DateTime.UtcNow;
}
}
Then here is how I am creating a belong to relation using other models. 然后,这是我如何使用其他模型创建属于关系。
[Table("BaseClientsToUsers")]
public class ClientToUser : ModelDefault
{
[ForeignKey("User")]
public int UserID { get; set; }
[ForeignKey("Client")]
public int ClientId { get; set; }
[ForeignKey("Team")]
public int DefaultTeamId { get; set; }
public DateTime? JoinedAt { get; set; }
public bool IsActive { get; set; }
public virtual User User { get; set; }
public virtual Client Client { get; set; }
public virtual Team Team { get; set; }
public ClientToUser()
{
DateTime UtcNow = DateTime.UtcNow;
IsActive = true;
CreatedAt = UtcNow;
LastUpdatedAt = UtcNow;
}
[Table("BaseTeams")]
public class Team : ModelDefault
{
[MaxLength(250)]
public string Name { get; set; }
[ForeignKey("Client")]
public int ClientId { get; set; }
public bool IsActive { get; set; }
public virtual Client Client { get; set; }
public Team()
{
DateTime UtcNow = DateTime.UtcNow;
IsActive = true;
CreatedAt = UtcNow;
LastUpdatedAt = UtcNow;
}
}
But, when I try to update my databases I get the following error 但是,当我尝试更新我的数据库时,我收到以下错误
Introducing FOREIGN KEY constraint 'FK_dbo.BaseTeams_dbo.BaseClients_ClientId' on table 'BaseTeams' may cause cycles or multiple cascade paths. 在表'BaseTeams'上引入FOREIGN KEY约束'FK_dbo.BaseTeams_dbo.BaseClients_ClientId'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 Could not create constraint or index. 无法创建约束或索引。 See previous errors. 查看以前的错误。
I am not really sure what could be causing the error but it seems it is because I am creating multiple Foreign keys to the same `Clients model. 我不确定是什么原因导致错误,但似乎是因为我正在为同一个`Clients模型创建多个外键。
How can I fix this error? 我该如何解决这个错误?
Hello @Mike A When I started MVC I got this error too, so you need aditional tables that connects your DB items. 你好@Mike A当我启动MVC时我也遇到了这个错误,所以你需要连接数据库项目的附加表。 So try connect your database items with tables like that: Here is my working example: 因此,尝试使用这样的表连接数据库项:这是我的工作示例:
[Table("Products")]
public class Product
{
[Key]
public string Id { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal InternalPrice { get; set; }
public string Url { get; set; }
}
[Table("Categories")]
public class Category
{
[Key]
public string Id { get; set; }
[Required]
public string Name { get; set; }
public string Url { get; set; }
}
[Table("ProductCategories")]
public class ProductCategory
{
[Key]
[Column(Order = 0)]
public string ProductId { get; set; }
[Key]
[Column(Order = 1)]
public string CategoryId { get; set; }
public virtual Category Category { get; set; }
}
So you can connect your items without problems hope this will help you. 因此,您可以毫无问题地连接您的物品,希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.