简体   繁体   English

如何在ASP.NET MVC中使用实体框架在父表中创建外键并将其分配给子表

[英]How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc

I'm developing a website using asp.net mvc with entity framework. 我正在使用带有实体框架的asp.net mvc开发网站。 I have an existing database and the relationship is shown below. 我有一个现有的数据库,其关系如下所示。

Parent table: 父表:

public class Accounts
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public int AddressId { get; set; }

    public virtual Addresses Address { get; set; }

    //Other properties...
}

Child table: 子表:

 public class Addresses
 {
    [Key, ForeignKey("Account")]
    public int AddressId { get; set; }

    public virtual Accounts Account { get; set; }

    //Other properties...
 }

//Create user logic: //创建用户逻辑:

public void CreateUser(UserModel model)
{
        Addresses newAddress = new Addresses()
        {
            ShippingFirstName = "1",
            ShippingLastName = "1",
            ShippingAddress = "1",
            ShippingAddress2 = "1",
            ShippingCity = "1",
            ShippingState = "1",
            ShippingZip = "1",
            ShippingCountry = "1",
            ShippingPhone = "1",
        };

        Accounts newAccount = new Accounts
        {
            UserFirstName = "a",
            UserLastName = "a",
            UserEmail = model.UserEmail,
            UserPassword = model.UserPassword,
            AddressId = 1,
            UserType = "User",
            Address = newAddress
        };
        context.Accounts.Add(newAccount);
        context.SaveChanges();
}

When I create the new user using the CreateUser function, the AddressId(primary key) in the Addresses table is the same as the AccountId(primary key, identity column) in the Accounts table after the data is inserted into the database, which means that entity framework uses the UserId as the foreign key and assigns it to the Addresses table. 当我使用CreateUser函数创建新用户时,将数据插入数据库后,Addresses表中的AddressId(主键)与Accounts表中的AccountId(主键,标识列)相同。实体框架使用UserId作为外键,并将其分配给Addresses表。 However, in my database, the foreign key is the AddressId in the Accounts table and points to the AddressId in the Addresses table. 但是,在我的数据库中,外键是Accounts表中的AddressId,并指向Addresses表中的AddressId。

Question: 题:

  1. Am I setting up the relationship in the entity class correctly? 我是否在实体类中正确设置了关系?

  2. How can I tell entity framework that it should use the AddressId in Accounts table as the foreign key, and assigns it to the AddressId in the Addresses table after context.Accounts.Add(newAccount) and context.SaveChanges()? 我如何告诉实体框架它应该使用Accounts表中的AddressId作为外键,并在context.Accounts.Add(newAccount)和context.SaveChanges()之后将其分配给Addresses表中的AddressId?

  3. How should I generate the foreign key(AddressId) in the Accounts table? 我应该如何在Accounts表中生成外键(AddressId)?

[ForeignKey("Account")] makes AddressId take Accounts 's primary key, which is UserId . [ForeignKey("Account")]使AddressId成为Accounts的主键,即UserId

Split that: 拆分:

public class Addresses
{
    [Key]
    public int AddressId { get; set; }

    [ForeignKey("Account")]            // Not necessary
    public int AccountId { get; set; } // Necessary

    public virtual Accounts Account { get; set; }

    // Other properties...
}

You should set the primary key in an entity: 您应该在实体中设置主键:

public void CreateUser(UserModel model)
{
    Addresses newAddress = new Addresses()
    {
        ShippingFirstName = "1",
        ShippingLastName = "1",
        ShippingAddress = "1",
        ShippingAddress2 = "1",
        ShippingCity = "1",
        ShippingState = "1",
        ShippingZip = "1",
        ShippingCountry = "1",
        ShippingPhone = "1",
        AddressId = 1 // Set primary key
    };

    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ASP.NET MVC中的Entity Framework将记录插入到具有外键的表中 - How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC asp.net mvc-使用实体框架6在第三个表中插入多个具有父级外键的记录 - asp.net mvc - Inserting multiple records in the third table having foreign keys to parent using entity framework 6 我如何在Asp.Net MVC中使用Entity Framework和Web Api获取外键表数据 - How do i get foreign key table data using Entity Framework and Web Api in Asp.Net MVC 实体框架未将父ID映射为子表中的外键 - Entity Framework not mapping parent id as foreign key in child table 没有实体框架外键关系的ASP.NET MVC - Asp.net mvc without entity framework foreign key relationships 如何在asp.net MVC 4中使用子表中的数据显示父表的数据 - How to display the data of the parent table using the data in the child table in asp.net MVC 4 ASP.NET MVC 父子视图模型和实体框架 - ASP.NET MVC Parent-Child with ViewModel and Entity Framework 首先在ASP.net MVC实体框架代码中创建外键关系 - Create Foreign Key Relationship in ASP.net MVC Entity Framework Code First 使用实体框架的代码优先方法在ASP.NET MVC应用程序中缺少外键关系 - Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework ASP.NET MVC如何访问Entity Framework生成的外键? - ASP.NET MVC How to access Entity Framework generated foreign key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM