C# 如何使用实体框架从多个其他模型创建与一个模型的外部关系?
我正在尝试使用ASP.NET MVC framework和Entity framework 6创建我的第一个应用程序 我选择使用代码优先的方法,并从定义模型开始 我有一个名为C# 如何使用实体框架从多个其他模型创建与一个模型的外部关系?,c#,asp.net,asp.net-mvc,entity-framework,entity,C#,Asp.net,Asp.net Mvc,Entity Framework,Entity,我正在尝试使用ASP.NET MVC framework和Entity framework 6创建我的第一个应用程序 我选择使用代码优先的方法,并从定义模型开始 我有一个名为Client的模型,其标识属性名为Id。我有多个模型,其属性名为ClientId。ClientId属性应该具有指向客户机的虚拟链接 下面是我的客户机模型的外观 [Table("clients")] public class Client { [Key] public int id { get; set; }
Client
的模型,其标识属性名为Id
。我有多个模型,其属性名为ClientId
。ClientId
属性应该具有指向客户机的虚拟链接
下面是我的客户机模型的外观
[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;
}
}
下面是我如何使用其他模型创建归属关系
[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;
}
}
但是,当我尝试更新我的数据库时,我得到了以下错误
引入外键约束
表“BaseTeams”中的“FK_dbo.BaseTeams_dbo.BaseClients_ClientId”可以
导致循环或多个级联路径。指定在删除时不执行任何操作或
更新时不执行任何操作,或修改其他外键约束。能够
不创建约束或索引。请参阅前面的错误
我不确定是什么导致了错误,但似乎是因为我正在为同一个“客户机”模型创建多个外键
如何修复此错误?你好@Mike A当我启动MVC时,我也遇到了此错误,因此您需要连接数据库项的传统表。
因此,请尝试将数据库项与如下表连接:
以下是我的工作示例:
[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; }
}
因此,您可以毫无问题地连接项目,希望这能对您有所帮助。你好@Mike A当我启动MVC时,我也遇到了这个错误,因此您需要连接数据库项目的传统表。
因此,请尝试将数据库项与如下表连接:
以下是我的工作示例:
[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; }
}
因此,您可以毫无问题地连接您的项目,希望这将对您有所帮助。您检查并确认了吗?感谢您提供的资源。我仍然不清楚如何在上述模型中创建循环级联。我所要做的就是创建hadOne、BelongsTo和hasmanyrelationi我试图禁用所有On-Delete级联,但我似乎做不好。下面是我添加到上下文类protected override void on modellcreating(DbModelBuilder modelBuilder){modelBuilder.Entity().HasKey(p=>p.Client).WillCascadeOnDelete(false);}
中的内容,但我现在在WillCascadeOnDelete下面有红线!你查过了吗?谢谢你提供的资源。我仍然不清楚如何在上述模型中创建循环级联。我所要做的就是创建hadOne、BelongsTo和hasmanyrelationi我试图禁用所有On-Delete级联,但我似乎做不好。下面是我添加到上下文类protected override void on modellcreating(DbModelBuilder modelBuilder){modelBuilder.Entity().HasKey(p=>p.Client).WillCascadeOnDelete(false);}
中的内容,但我现在在WillCascadeOnDelete下面有红线!