簡體   English   中英

覆蓋多對多關系的代碼優先命名約定

[英]Override code-first naming convention for many-to-many relationships

我有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; }
}

我在這兩個表之間有一個經典映射(名稱為UserRule ,其復合主鍵由2個列( 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

現在在第二個請求上,我找不到僅在UserRule表上使用“ UserId ”的UserRule

我怎樣才能做到這一點?

(編輯)我嘗試刪除以下3條約定,但到目前為止它什么都沒有改變:

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

您的代碼是精確的副本還是偽代碼?

准確地粘貼您的代碼,我得到了相同的結果-

但是,您要兩次映射表的右部分:

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

我將top命令更改為映射到左鍵

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

並為我修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM