[英]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: 题:
Am I setting up the relationship in the entity class correctly? 我是否在实体类中正确设置了关系?
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?
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.