实体框架 - 代码优先关系:一对一

时间:2011-08-21 02:52:53

标签: asp.net-mvc-3 c#-4.0 entity-framework-4

我有两个表用户和公司:

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为null。有人可以解释我的一对一关系是否在用户和公司之间搞砸了吗?

** * ** * ** * 的** * 编辑 * ** * ** * ** * ** * ***

    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:   具有模式“dbo”和表的EntitySet“UserApplication”   'ApplicationAdministrators'已经定义。每个EntitySet必须   请参阅唯一的架构和表格。

     

第15行:公共IQueryable用户   第16行:{   第17行:获取{return context.Users.Include(“AdministratorApplications”)。包含(“公司”); }   第18行:}   第19行:

1 个答案:

答案 0 :(得分:3)

您需要Company属性virtual

public class User
{
    // Properties

    public virtual Company Company { get; set; }

}

如果你不想让它virtual,你需要告诉EF使用Company方法加载Include属性。

通过使属性virtual EF将延迟加载属性。但是,当您访问Company对象时,您正在访问user属性,那么您可以使用Include方法来急切加载Company属性。

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