1
我有一個EF 4.1模型,從我的數據庫中生成PERSON和ADDRESS兩個表。實體框架與對象與集合,與Automapper,更新異常
//此方法
public void Update(IPerson person)
{
var personDb = _dataContext.PERSON.SingleOrDefault(x => x.ID == person.Id);
Mapper.Map<Person, PERSON>((Person)person, personDb);
_dataContext.SaveChanges();
}
但是當我刪除Automapper映射的結尾。可以忽略(),我得到這個異常:
的EntityCollection不能因爲關係經理進行初始化EntityCollection所屬的對象已經附加到ObjectContext。只應調用InitializeRelatedCollection方法來在對象圖的反序列化過程中初始化新的EntityCollection。
我希望在向現有地址添加新地址時保存個人和地址。
有什麼想法?
感謝,
public void AutomapperInit()
{
Mapper.CreateMap<Person, PERSON>()
.ForMember(x => x.ADDRESS, opt => opt.Ignore());
Mapper.CreateMap<PERSON, Person>()
.ForMember(dest => dest.Address, option => option.MapFrom(src => src.ADDRESS.Select(address => Mapper.Map<ADDRESS, Address>(address)).ToList()));
Mapper.CreateMap<Address, ADDRESS>();
Mapper.CreateMap<ADDRESS, Address>()
.ForMember(dest => dest.Rue, option => option.MapFrom(src => src.STREET));
}
public interface IPerson
{
int Id { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
ICollection<IAddress> Address { get; set; }
}
public interface IAddress
{
string Rue { get; set; }
string Number { get; set; }
int PersonId { get; set; }
}
class Person : IPerson
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<IAddress> Address { get; set; }
}
class Address : IAddress
{
public string Rue { get; set; }
public string Number { get; set; }
public int PersonId { get; set; }
}
這不是personDB的問題,它是集合personDb的集合 – 2012-07-25 11:36:09