我使用的是EF 6 Code First MVC plus标识
我自定义了Identity framework中定义的标准ApplicationUser类,当我尝试通过网页注册新用户时,EF尝试插入数据库中已经存在的实体。
//some code above here
using (ApplicationDbContext db = new ApplicationDbContext())
{
// I load the City object form db based on the model passed to current method
City city = db.Countries.Include("States.Cities")
.First(c => c.Code == model.CountryCode)
.States.First(s => s.Id == model.StateId)
.Cities.First(ci => ci.Name == model.CityName);
// I create a new Employee and a new Company and Company has an Address
// referring to the City I loaded from db
Employee employee = new Employee
{
FirstName = model.FirstName,
LastName = model.LastName,
Company = new Company
{
Name = model.CompanyName,
OfficePhone = model.OfficePhone,
Address = new Address { City = city, Street = model.Street, ZipCode = model.ZipCode }
}
};
// This is part of Identity framework code
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
// my custom code: I assign employee to the standard ApplicationUser class
UserProfile = employee
};
// This is going to save the new user to database,
// but it tries to insert a new Country object into db.
// Note that City class has a property of type State
// and State class has a property type of Country
var result = await UserManager.CreateAsync(user, model.Password);
// more code below
根据我得到的错误,听起来EF正在将Country实体插入db。我猜当EF向db添加地址时会发生这种情况。在我的代码中,Address是一个新对象,但City、State和Country已经存在了,您可以看到我一开始是从db加载City的。因此,我尝试使用以下代码,但没有任何帮助:
db.Entry(employee.Company.Address.City).State = EntityState.Unchanged;
很抱歉,代码太长了,但我尝试将其最小化。
这是我的模型:
public class Country
{
string Code;
string Name;
List<State> States;
}
public class State
{
int Id;
string Name;
List<City> Cities;
}
public class City
{
int Id;
string Name;
State State;
}
public class Address
{
string Street;
string ZipCode;
City City;
}
public class Company
{
string Name;
string OfficePhone;
Address Address;
}