1

我的域模型中有一个对象,User其中包含一个配置设置列表。它看起来像这样:

public class User
{
    public int ID { get; set; }

    /* various user properties */

    public List<ConfigurationSetting> Settings { get; set; }
}

配置设置具有以下定义:

public class ConfigurationSetting
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public MasterAccount Account { get; set; }
}

如您所见,配置类中没有反向导航属性。这是因为设置附加到 aMasterAccount并且可选地应用于 a User,因此需要一个映射表。

我正在尝试在我的配置中创建关系,UserConfiguration如下所示:

            HasMany(x => x.Settings)
            .WithMany()
            .Map(m =>
            {
                m.ToTable("MapUserSettings");
                m.MapLeftKey("UserId");
                m.MapRightKey("SettingId");
            });

请注意,使用以下内容重命名 ID:

        HasKey(x => x.ID);
        Property(x => x.ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("UserId");

当 EF Code First 尝试创建模型时,我得到以下信息:

'FK_dbo.MapUserSettings_dbo.Settings_SettingId' on table 'MapUserSettings' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

如何正确配置级联?

4

1 回答 1

2

I've tried to reproduce your problem given the information you've provided, and I can't get EF to complain about building the model.

The one thing you didn't provide was a definition for MasterAccount, so I have to believe that you have some relationship coded there that is causing the circular dependency.

If the issue is simply that you don't want cascading delete for one/all of the relationships, you can disable it using the Fluent API by adding

.WillCascadeOnDelete(false)

to the relationship's configuration. For example:

   modelBuilder.Entity<User>()
        .HasMany(x => x.Settings)
        .WithRequired()
        .WillCascadeOnDelete(false)

Below is what I coded up that works fine in my environment:

public class User
{
    public int ID { get; set; }

    /* various user properties */

    public List<ConfigurationSetting> Settings { get; set; }
}

public class ConfigurationSetting
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public MasterAccount Account { get; set; }
}

public class MasterAccount
{
    public int ID { get; set; }
    public List<ConfigurationSetting> Settings { get; set; }
    public User User { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<MasterAccount> MasterAccounts { get; set; }
    public DbSet<ConfigurationSetting> Settings { get; set; }
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
                .HasKey(x => x.ID);

        modelBuilder.Entity<User>()
            .Property(x => x.ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("UserId");

        modelBuilder.Entity<User>()
            .HasMany(x => x.Settings)
            .WithMany()
            .Map(m =>
             {
                 m.ToTable("MapUserSettings");
                 m.MapLeftKey("UserId");
                 m.MapRightKey("SettingId");
             });

        base.OnModelCreating(modelBuilder);
    }
}
于 2013-06-18T02:02:55.503 回答