【发布时间】:2011-08-21 02:52:53
【问题描述】:
我有两个表用户和公司:
public class User
{
// Properties
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public long AgencyId { get; set; }
public Company Company { get; set; }
// Custom Propreties
[ScaffoldColumn(false)]
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
public class Company
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
配置是这样的……
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
this.HasKey(x => x.Id);
this.Property(x => x.Id);
this.Property(x => x.FirstName).IsRequired();
this.Property(x => x.LastName).IsRequired();
this.Property(x => x.Username).IsRequired();
this.Property(x => x.CompanyId).IsRequired();
this.HasRequired(user => user.Company).WithMany().HasForeignKey(user => user.CompanyId);
}
}
public class CompanyConfiguration : EntityTypeConfiguration<Company>
{
public CompanyConfiguration()
{
this.ToTable("Companies");
this.HasKey(x => x.Id);
this.Property(x => x.Id);
this.Property(x => x.Name).IsRequired();
this.HasMany(company => company.Users).WithRequired().HasForeignKey(user => user.CompanyId);
}
}
如果我创建一个包含公司的视图来显示每家公司并将一列设为公司中的用户数,那么视图将按预期呈现,显示每家公司中的用户数。但是,当我尝试在视图中显示每个用户并在列中显示 Company.Name 时,它会说 Company 为空。如果我的一对一关系在用户和公司之间搞砸了,有人可以解释一下吗?
************ 编辑 *********** *****
public UserConfiguration()
{
this.HasKey(x => x.Id);
this.Property(x => x.Id);
this.Property(x => x.FirstName).IsRequired();
this.Property(x => x.LastName).IsRequired();
this.Property(x => x.Username).IsRequired();
this.Property(x => x.CompanyId).IsRequired();
this.HasRequired(user => user.Company).WithMany().HasForeignKey(user => user.CompanyId);
this.HasMany(user => user.AdministratorApplications)
.WithMany(application => application.Administrators)
.Map(map =>
{
map.ToTable("ApplicationAdministrators");
map.MapLeftKey("ApplicationId");
map.MapRightKey("UserId");
});
}
public ApplicationConfiguration()
{
this.HasKey(x => x.Id);
this.Property(x => x.Name).IsRequired();
this.Property(x => x.Accronym).IsRequired();
this.Property(x => x.Description);
this.HasMany(application => application.Administrators)
.WithMany(user => user.AdministratorApplications)
.Map(map =>
{
map.ToTable("ApplicationAdministrators");
map.MapLeftKey("UserId");
map.MapRightKey("ApplicationId");
});
}
public ApplicationAdministratorConfiguration()
{
this.ToTable("ApplicationAdministrators");
this.HasKey(x => x.Id);
this.Property(x => x.Id);
this.Property(x => x.ApplicationId).IsRequired();
this.Property(x => x.UserId).IsRequired();
this.HasRequired(appAdmin => appAdmin.Application).WithMany().HasForeignKey(appAdmin => appAdmin.ApplicationId);
this.HasRequired(appAdmin => appAdmin.User).WithMany().HasForeignKey(appAdmin => appAdmin.UserId);
}
这是 ApplicationAdministrator 类
public class ApplicationAdministrator
{
[Column("Id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[HiddenInput]
public long Id { get; set; }
[Display(Name = "Application")]
public long ApplicationId { get; set; }
public virtual Application Application { get; set; }
[Display(Name = "Administrator")]
public long UserId { get; set; }
public virtual User User { get; set; }
}
最后是错误
指定的架构无效。错误:(144,6):错误 0019: EntitySet 'UserApplication' 与模式 'dbo' 和表 'ApplicationAdministrators' 已被定义。每个 EntitySet 必须 引用一个唯一的模式和表。
第 15 行:公共 IQueryable 用户 第 16 行:{ 第 17 行:get { return context.Users.Include("AdministratorApplications").Include("Company"); } 第 18 行:} 第 19 行:
【问题讨论】:
标签: asp.net-mvc-3 c#-4.0 entity-framework-4