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?
As far as I see I think:if you are using EntityFramework,your two models' defination is quite right,and in fact you don't need to define another Address for the MainAdress——for your own since your main email comes from all of the specific email addresses……So
your Address class should have a boolean type that tells whether the Email is a Main email address or not。That's fine。
【Snippet of sample codes】
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 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; }
public boolean IsMain{get;set;} //Add this property……
}
public class NTMAAContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
public DbSet<Address> Addresses { get; set; }
}
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
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Is this relationship even possible to define within Entity Framework?
May 25, 2012 01:56 AM|LINK
Hello rodd_harris:)
As far as I see I think:if you are using EntityFramework,your two models' defination is quite right,and in fact you don't need to define another Address for the MainAdress——for your own since your main email comes from all of the specific email addresses……So your Address class should have a boolean type that tells whether the Email is a Main email address or not。That's fine。
【Snippet of sample codes】
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 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; } public boolean IsMain{get;set;} //Add this property…… } public class NTMAAContext : DbContext { public DbSet<Account> Accounts { get; set; } public DbSet<Address> Addresses { get; set; } }EF4.1