简体   繁体   English

如何使用Entity框架从多个其他模型创建一个模型的外来关系?

[英]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.

相关问题 如何在Entity Framework中使用流利的API为一对一..one关系指定外键属性 - How to specify foreign key property for one to zero..one relation using fluent api in Entity Framework 使用实体框架,如何在两个模型上添加外键以相互引用 - Using Entity Framework, how can I add a foreign key on two models to reference each other MVC实体框架-来自一个字段中多个表的外键 - MVC Entity Framework - Foreign Keys from multiple tables in one field 具有多个相同外键的实体框架模型 - Entity Framework Model With Multiple Same Foreign Keys 在实体框架中需要多个外键之一 - Require One of Multiple Foreign Keys in Entity Framework 如何使用Entity Framework将多个对象添加到外表 - How to add multiple objects to a foreign table using Entity Framework 如何在实体框架中创建将一对一关系更改为一对多的迁移? - How to create migration which change one to one relation to one to many in entity framework? 如何关闭实体框架创建外键 - How to close Entity Framework create foreign key 如何在实体框架核心中定义一个共享的实体并使其他实体具有一对多的关系? - How to define a shared enity and make other entitis have one to many relation to it in Entity Framework Core? 实体框架一对一关系 - Entity framework one to one relation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM