public class Account
{
public Account()
{
Addresses = new List<Address>();
}
public string Id { get; set; }
public string Firstname { get; set; }
public string AcctName { get; set; }
public string Lname { get; set; }
public string AcctStatCd { get; set; }
public string MCatCd { get; set; }
public string Name { get; set; }
public string CtryCd { get; set; }
public string HQFldCd { get; set; }
public ICollection<Address> Addresses { get; private set; }
public Guid MailingAddressId {get; set;}
public Address MailingAddress {get; set;}
}
public class Address
{
public Guid Id { get; set; }
public string AccountId { get; set;}
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string StateProvinceCode { get; set; }
public string CountryCode { get; set; }
public string PostalCode { get; set; }
public int AddressTypeCode { get; set; }
}
The idea here is that an Account has many addresses but only one is the default mailing address. A mailing address is required (although I may just have to enforce that as part of my business rules.)
I have tried all sorts of mapping scenarios but can not seem to get this to work once I add in the MailingAddress piece. Here is my latest attempt:
public class NTMAAContext : DbContext
{
public IDbSet<Account> Accounts { get; set; }
public IDbSet<Address> Addresses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
this.Configuration.LazyLoadingEnabled = false;
modelBuilder.Configurations.Add(new ConfigureAccounts());
modelBuilder.Configurations.Add(new ConfigureAddresses());
}
public NTMAAContext(string connectionString) : base(connectionString)
{}
}
public class ConfigureAccounts : EntityTypeConfiguration<Account>
{
public ConfigureAccounts()
{
ToTable("missacct");
HasKey(m => m.Id);
Property(m => m.Id).HasColumnName("acctno");
Property(m => m.Firstname).HasColumnName("Fname");
Property(m => m.MailingAddressId).HasColumnName("mailingaddressguid");
HasMany(m => m.Addresses).WithRequired().HasForeignKey(a => a.AccountId);
HasRequired(m => m.MailingAddress).WithRequiredPrincipal();
}
}
public class ConfigureAddresses : EntityTypeConfiguration<Address>
{
public ConfigureAddresses()
{
ToTable("missaddr");
HasKey(m => m.Id);
Property(m => m.Id).HasColumnName("guid");
Property(m => m.AccountId).HasColumnName("acctno");
Property(m => m.CountryCode).HasColumnName("ctrycd");
Property(m => m.Line1).HasColumnName("addr1");
Property(m => m.Line2).HasColumnName("addr2");
Property(m => m.PostalCode).HasColumnName("zip");
Property(m => m.StateProvinceCode).HasColumnName("stprov");
}
}
}
I have tried adding .Map to the .WithRequiredPrincipal but that hasn't really worked well.
So, depending on what I try, I either get an error when queyring the data or when trying to add a new account. Sometimes I'm getting something about unable to determine order ... (usually when I have the .WithRequiredPrincipal stuff in place) or I get an
error about an invalid key Account_Id (if I don't add any kind of .Map() to .WithRequiredPrincipal) or acctno already defined if I do try to use a mapping.
Does anyone know how to accomplish something like this? Is it even possible?
rodd_harris
Member
82 Points
80 Posts
Is this relationship even possible to define within Entity Framework?
May 23, 2012 09:20 PM|LINK
I have the following classes:
public class Account { public Account() { Addresses = new List<Address>(); } public string Id { get; set; } public string Firstname { get; set; } public string AcctName { get; set; } public string Lname { get; set; } public string AcctStatCd { get; set; } public string MCatCd { get; set; } public string Name { get; set; } public string CtryCd { get; set; } public string HQFldCd { get; set; } public ICollection<Address> Addresses { get; private set; } public Guid MailingAddressId {get; set;} public Address MailingAddress {get; set;} } public class Address { public Guid Id { get; set; } public string AccountId { get; set;} public string Line1 { get; set; } public string Line2 { get; set; } public string City { get; set; } public string StateProvinceCode { get; set; } public string CountryCode { get; set; } public string PostalCode { get; set; } public int AddressTypeCode { get; set; } }The idea here is that an Account has many addresses but only one is the default mailing address. A mailing address is required (although I may just have to enforce that as part of my business rules.)
I have tried all sorts of mapping scenarios but can not seem to get this to work once I add in the MailingAddress piece. Here is my latest attempt:
public class NTMAAContext : DbContext { public IDbSet<Account> Accounts { get; set; } public IDbSet<Address> Addresses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { this.Configuration.LazyLoadingEnabled = false; modelBuilder.Configurations.Add(new ConfigureAccounts()); modelBuilder.Configurations.Add(new ConfigureAddresses()); } public NTMAAContext(string connectionString) : base(connectionString) {} } public class ConfigureAccounts : EntityTypeConfiguration<Account> { public ConfigureAccounts() { ToTable("missacct"); HasKey(m => m.Id); Property(m => m.Id).HasColumnName("acctno"); Property(m => m.Firstname).HasColumnName("Fname"); Property(m => m.MailingAddressId).HasColumnName("mailingaddressguid"); HasMany(m => m.Addresses).WithRequired().HasForeignKey(a => a.AccountId); HasRequired(m => m.MailingAddress).WithRequiredPrincipal(); } } public class ConfigureAddresses : EntityTypeConfiguration<Address> { public ConfigureAddresses() { ToTable("missaddr"); HasKey(m => m.Id); Property(m => m.Id).HasColumnName("guid"); Property(m => m.AccountId).HasColumnName("acctno"); Property(m => m.CountryCode).HasColumnName("ctrycd"); Property(m => m.Line1).HasColumnName("addr1"); Property(m => m.Line2).HasColumnName("addr2"); Property(m => m.PostalCode).HasColumnName("zip"); Property(m => m.StateProvinceCode).HasColumnName("stprov"); } } }I have tried adding .Map to the .WithRequiredPrincipal but that hasn't really worked well.
So, depending on what I try, I either get an error when queyring the data or when trying to add a new account. Sometimes I'm getting something about unable to determine order ... (usually when I have the .WithRequiredPrincipal stuff in place) or I get an error about an invalid key Account_Id (if I don't add any kind of .Map() to .WithRequiredPrincipal) or acctno already defined if I do try to use a mapping.
Does anyone know how to accomplish something like this? Is it even possible?
Thanks for your help.
EF4.1