【问题标题】:Entity Framework - Code First relationship: one-to-one实体框架 - 代码优先关系:一对一
【发布时间】: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


    【解决方案1】:

    你需要使Company属性virtual

    public class User
    {
        // Properties
    
        public virtual Company Company { get; set; }
    
    }
    

    如果您不想使其成为 virtual,则需要告诉 EF 使用 Include 方法加载 Company 属性。

    通过使属性virtual EF 将延迟加载该属性。但是如果您在访问user 对象时访问Company 属性,那么您可以使用Include 方法来预先加载Company 属性。

    var users = context.Users.Include(user => user.Company).Where(/*conditions*/);
    

    【讨论】:

    • 您不能将 lambda 表达式与 Include 一起使用。用虚拟修饰公司属性会引发此错误...“已经有一个打开的 DataReader 与此命令关联,必须先关闭。”
    • @bdparrish 您可以发布您的 LINQ 查询以及您如何访问返回的结果吗?
    • 现在知道了,只需将 MultipleActiveResultSets=true 属性添加到我的连接中,这是我以前从未做过的。
    • @bdparrish MultipleActiveResultSets=true 可以解决问题,但您会遇到Select N+1 problem。您可能需要另一个 Include 语句。
    • 是的,我有context.Users.Include("Applications").Include("Company");。现在我在保存到上下文时遇到了问题。它在引用 ApplicationAdministrators1 表时引发错误,但我没有该表。它会因为 MARS 而创建多个表作为临时表吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多