リバース エンジニアリング ツール (Entity Framework Power Tools) によって生成された次のクラスがあります。
public class User
{
public User()
{
this.Areas = new List<Area>();
this.Areas1 = new List<Area>();
this.Areas2 = new List<Area>();
this.Areas3 = new List<Area>();
this.Companies = new List<Company>();
}
public int UserID { get; set; }
public string Name { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public bool ActiveDirectory { get; set; }
public string Language { get; set; }
public string Email { get; set; }
public System.DateTime ValidFrom { get; set; }
public System.DateTime ValidTo { get; set; }
public bool AllAreas { get; set; }
public virtual ICollection<Area> Areas { get; set; }
public virtual ICollection<Area> Areas1 { get; set; }
public virtual ICollection<Area> Areas2 { get; set; }
public virtual ICollection<Area> Areas3 { get; set; }
public virtual ICollection<Company> Companies { get; set; }
}
public class Area
{
public Area()
{
this.Users = new List<User>();
}
public int AreaID { get; set; }
public string AreaNL { get; set; }
public string AreaFR { get; set; }
public string ExternalID { get; set; }
public int CompanyID { get; set; }
public int AreaManagerID { get; set; }
public int DistrictManagerID { get; set; }
public int VAID { get; set; }
public virtual User User { get; set; }
public virtual Company Company { get; set; }
public virtual User User1 { get; set; }
public virtual User User2 { get; set; }
public virtual ICollection<User> Users { get; set; }
}
マッピング ファイルのコンストラクタは次のとおりです。
public UserMap()
{
// Primary Key
this.HasKey(t => t.UserID);
// Properties
this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(100);
this.Property(t => t.Login)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.Password)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.Language)
.IsRequired()
.HasMaxLength(2);
this.Property(t => t.Email)
.IsRequired()
.HasMaxLength(150);
// Table & Column Mappings
this.ToTable("AreaManager");
this.Property(t => t.UserID).HasColumnName("UserID");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.Login).HasColumnName("Login");
this.Property(t => t.Password).HasColumnName("Password");
this.Property(t => t.ActiveDirectory).HasColumnName("ActiveDirectory");
this.Property(t => t.Language).HasColumnName("Language");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.ValidFrom).HasColumnName("ValidFrom");
this.Property(t => t.ValidTo).HasColumnName("ValidTo");
this.Property(t => t.AllAreas).HasColumnName("AllAreas");
}
public AreaMap()
{
// Primary Key
this.HasKey(t => t.AreaID);
// Properties
this.Property(t => t.AreaNL)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.AreaFR)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.ExternalID)
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("Area");
this.Property(t => t.AreaID).HasColumnName("AreaID");
this.Property(t => t.AreaNL).HasColumnName("AreaNL");
this.Property(t => t.AreaFR).HasColumnName("AreaFR");
this.Property(t => t.ExternalID).HasColumnName("ExternalID");
this.Property(t => t.CompanyID).HasColumnName("CompanyID");
this.Property(t => t.AreaManagerID).HasColumnName("AreaManagerID");
this.Property(t => t.DistrictManagerID).HasColumnName("DistrictManagerID");
this.Property(t => t.VAID).HasColumnName("VAID");
// Relationships
this.HasMany(t => t.Users)
.WithMany(t => t.Areas3)
.Map(m =>
{
m.ToTable("UserArea");
m.MapLeftKey("AreaID");
m.MapRightKey("UserID");
});
this.HasRequired(t => t.User)
.WithMany(t => t.Areas)
.HasForeignKey(d => d.AreaManagerID);
this.HasRequired(t => t.Company)
.WithMany(t => t.Areas)
.HasForeignKey(d => d.CompanyID);
this.HasRequired(t => t.User1)
.WithMany(t => t.Areas1)
.HasForeignKey(d => d.DistrictManagerID);
this.HasRequired(t => t.User2)
.WithMany(t => t.Areas2)
.HasForeignKey(d => d.VAID);
}
今、私が次のことをしようとすると:
using (var ctx = new FCTestContext())
{
// Step 1: add user
var user = new User
{
ActiveDirectory = false,
Login = "Joske",
Email = "tst@abc.com",
Password = "sdfsdff",
Name = "JoskeVermeulen",
Language = "NL"
};
// Step 2: add an Area
Company company = new Company { Active = true, CompanyName = "CompanyTest" };
Area area = new Area
{
AreaFR = "Anvers",
AreaNL = "Antwerpen",
Company = company,
User = user
};
ctx.Areas.Add(area);
try
{
ctx.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (DbEntityValidationResult validationErrors in dbEx.EntityValidationErrors)
{
foreach (DbValidationError validationError in validationErrors.ValidationErrors)
{
//Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName,
validationError.ErrorMessage);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
for (Exception eCurrent = ex; eCurrent != null; eCurrent = eCurrent.InnerException)
{
Console.WriteLine(eCurrent.Message);
}
}
Console.ReadLine();
エラーが表示されます:
オブジェクト名 dbo.AreaManager が無効です。私は明らかにそのテーブルを持っておらず、追加するつもりもありません。マネージャーごとに 3 つの外部キーのみを提供する必要があり、ユーザーはすべての領域を表示できる必要があります。