开发者

Adding item to a list/collection (with MVC EF 4.1 code first)

开发者 https://www.devze.com 2023-04-09 22:16 出处:网络
Having problem with adding ite开发者_开发技巧m/object into a list/collection. This is what I\'ve done so far...

Having problem with adding ite开发者_开发技巧m/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.


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:

public void Apply(PersonInformation member)
{
    member.person.MemberId = CustomID.GenerateID();
    member.person.MemberTypeFlag = (int)Enum.Parse(typeof(EnumMemberType), "Person");
    member.person.DateApplied = DateTime.Now;

    member.person.person.Addresses.Add(member.billingAddress);
    person.Addresses.Add(member.premiseAddress);

    context.People.Add(member.person);
    context.SaveChanges();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消