我有反向工程工具(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个外键,用户也应该能够看到所有区域。
答案 0 :(得分:0)
在记下所有内容并在发布之后再次查看代码后,看到罪魁祸首的地方: - (。
它在行
this.ToTable("AreaManager");
由于某种原因,工具生成它就像在数据库中映射一样。将其更改为
this.ToTable("User");
让它发挥作用。