将项目添加到列表/集合(首先使用 MVC EF 4.1 代码)

发布于 2024-12-08 00:14:38 字数 2357 浏览 0 评论 0原文

将项目/对象添加到列表/集合中时遇到问题。

这就是我到目前为止所做的...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2024-12-15 00:14:38

为什么要创建一个新人和 2 个新地址,然后在 1 行之后重新分配变量?你的例子中有很多冗余代码。

您的代码基本上可以归结为以下内容:

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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文