0

我有 2 个具有多对多关系的实体,它们不遵循代码优先命名约定,我必须找到一种方法来覆盖它。

第一个实体:

public class User  
{
    public int UserId { get; set; }
    public string Username { get; set; }
    public virtual ICollection<Rule> { get; set; }
}

第二个实体:

public class Rule  
{
    public int RuleId { get; set; }
    public string Description { get; set; }
    public virtual ICollection<User> { get; set; }
}

我在这两个之间有一个带有经典映射的表(名称是由 2 个列( )和相应的外键UserRule组成的复合主键)UserId, RuleId

我使用的 dbcontext 是这个:

public class DataContext : DbContext
{
    public IDbSet<User> Users { get; private set; }
    public IDbSet<Rule> Rules { get; private set; }

    private const string ConnectionStringName = "connectionstring";

    public DataContext() : base(ConnectionStringName)
    {
        Configuration.LazyLoadingEnabled = true;

        Rules = Set<Rule>();
        Users = Set<User>();
    }

    protected override void OnModelCreating(DbModelBuilder builder)
    {
        // use this to disable database migrations and not use metadata inside database
        Database.SetInitializer<DataContext>(null);

        // setup MaestroAgentConfig to its table
        builder.Configurations.Add(new UserTable());

        // setup SecurityRule to its table
        builder.Configurations.Add(new RuleTable());

        base.OnModelCreating(builder);
    }

    public override int SaveChanges()
    {
        return base.SaveChanges();
    }
}

我拥有的映射表是:

public class UserTable : EntityTypeConfiguration<User>
{
    public UserProfileTable()
    {
        Property(x => x.UserId).HasColumnName("UserId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        HasMany(x => x.SecurityRules)
            .WithMany(x => x.UserProfiles)
            .Map(map =>
            {
                map.MapRightKey("UserId");
                map.MapRightKey("RuleId");
                map.ToTable("UserRule");
            });
        ToTable("User");
    }
}

和 :

public class RuleTable : EntityTypeConfiguration<Rule>
{
    public RuleTable()
    {
        Property(x => x.RuleId).HasColumnName("RuleId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        HasKey(x => x.RuleId);
        ToTable("Rule");
    }
}

然而,当 EF 查询数据库时,它看起来像这样:

exec sp_executesql N'
SELECT TOP (1) 
    [Extent1].[UserId] AS [UserId], 
    { other fields }
FROM 
    [dbo].[UserProfile] AS [Extent1]
WHERE 
    [Extent1].[UserName] = @p__linq__0',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'EMYODE\erick.girard'

和 :

exec sp_executesql N'
SELECT 
    [Extent2].[RuleId] AS [RuleId], 
    [Extent2].[Description] AS [Description]
FROM  
    [dbo].[UserRule] AS [Extent1]
INNER JOIN 
    [dbo].[Rule] AS [Extent2] ON [Extent1].[RuleId] = [Extent2].[RuleId]
WHERE 
    [Extent1].[User_UserId] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=37

现在在第二个请求中,我找不到UserId仅在UserRule桌子上使用“”的方法

我怎样才能做到这一点?

(编辑)我尝试删除以下 3 个约定,但到目前为止它没有改变任何东西:

builder.Conventions.Remove<PluralizingEntitySetNameConvention>();
builder.Conventions.Remove<NavigationPropertyNameForeignKeyDiscoveryConvention>();
builder.Conventions.Remove<TypeNameForeignKeyDiscoveryConvention>();
4

1 回答 1

0

您的代码是精确副本还是伪代码?

准确地粘贴您的代码,我得到了相同的结果 -

但是,您将映射表的右侧部分映射两次:

map.MapRightKey("UserId");
map.MapRightKey("RuleId");

我将顶部命令更改为映射到左键

map.MapLeftKey("UserId");
map.MapRightKey("RuleId");

并为我修好了。

于 2012-08-02T20:05:18.800 回答