将项目添加到列表/集合(首先使用 MVC EF 4.1 代码)
将项目/对象添加到列表/集合中时遇到问题。
这就是我到目前为止所做的...
public abstract class Member
{
[DisplayName("ID")]
public string MemberId { get; set; }
[DisplayName("Date Applied")]
public System.DateTime? DateApplied { get; set; }
[DisplayName("Date Membered")]
public System.DateTime? DateMembered { get; set; }
[DisplayName("Member Type")]
public int MemberTypeFlag { get; set; }
private ICollection<Address> _Addresses;
public virtual ICollection<Address> Addresses
{
get { return _Addresses ?? (_Addresses = new HashSet<Address>()); }
set { _Addresses = value; }
}
}
public class Person : Member
{
[DisplayName("Last Name")]
public string LastName { get; set; }
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Date Of Birth")]
public System.DateTime DateOfBirth { get; set; }
}
这就是我映射它的方式..
public class MapAddress : EntityTypeConfiguration<Address>
{
public MapAddress()
: base()
{
HasKey(a => a.MemberId).HasOptional(a => a.Member).WithMany(i => i.Addresses);
Property(p => p.Address1).HasColumnName("ADDRESS1");
Property(p => p.Address2).HasColumnName("ADDRESS2");
Property(p => p.City).HasColumnName("CITY");
Property(p => p.ZipCode).HasColumnName("ZIPCODE");
Property(p => p.AddressTypeFlag).HasColumnName("ADDRESS_TYPE_FFLG");
ToTable("MBR_ADDRESS");
}
}
并在保存时引发错误..
“一个或多个实体的验证失败。有关更多详细信息,请参阅“EntityValidationErrors”属性。”
这就是我添加到集合中的方式。
public void Apply(PersonInformation member)
{
Person person = new Person();
person = member.person;
person.MemberId = CustomID.GenerateID();
person.MemberTypeFlag = (int)Enum.Parse(typeof(EnumMemberType), "Person");
person.DateApplied = DateTime.Now;
Address baddress = new Address();
baddress = member.billingAddress;
Address paddress = new Address();
paddress = member.premiseAddress;
person.Addresses.Add(baddress);
person.Addresses.Add(paddress);
context.People.Add(person);
context.SaveChanges();
}
或者还有其他优雅的方式来保存具有多个地址的成员。
Having problem with adding item/object into a list/collection.
This is what I've done so far...
public abstract class Member
{
[DisplayName("ID")]
public string MemberId { get; set; }
[DisplayName("Date Applied")]
public System.DateTime? DateApplied { get; set; }
[DisplayName("Date Membered")]
public System.DateTime? DateMembered { get; set; }
[DisplayName("Member Type")]
public int MemberTypeFlag { get; set; }
private ICollection<Address> _Addresses;
public virtual ICollection<Address> Addresses
{
get { return _Addresses ?? (_Addresses = new HashSet<Address>()); }
set { _Addresses = value; }
}
}
public class Person : Member
{
[DisplayName("Last Name")]
public string LastName { get; set; }
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Date Of Birth")]
public System.DateTime DateOfBirth { get; set; }
}
And this is how I mapped it..
public class MapAddress : EntityTypeConfiguration<Address>
{
public MapAddress()
: base()
{
HasKey(a => a.MemberId).HasOptional(a => a.Member).WithMany(i => i.Addresses);
Property(p => p.Address1).HasColumnName("ADDRESS1");
Property(p => p.Address2).HasColumnName("ADDRESS2");
Property(p => p.City).HasColumnName("CITY");
Property(p => p.ZipCode).HasColumnName("ZIPCODE");
Property(p => p.AddressTypeFlag).HasColumnName("ADDRESS_TYPE_FFLG");
ToTable("MBR_ADDRESS");
}
}
And error raised upon saving..
"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."
This is how I add into a collection..
public void Apply(PersonInformation member)
{
Person person = new Person();
person = member.person;
person.MemberId = CustomID.GenerateID();
person.MemberTypeFlag = (int)Enum.Parse(typeof(EnumMemberType), "Person");
person.DateApplied = DateTime.Now;
Address baddress = new Address();
baddress = member.billingAddress;
Address paddress = new Address();
paddress = member.premiseAddress;
person.Addresses.Add(baddress);
person.Addresses.Add(paddress);
context.People.Add(person);
context.SaveChanges();
}
Or there are other elegant way to save a member with many address.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要创建一个新人和 2 个新地址,然后在 1 行之后重新分配变量?你的例子中有很多冗余代码。
您的代码基本上可以归结为以下内容:
Why are you creating a new person and 2 new addresses then reassigning the variable 1 line later? There's a lot of redundant code in your example.
Your code basically boils down to the following: