2013-04-20 36 views
1

我試圖鏈接列表的子和一個視頻對象的主要子級與刪除級聯。EF多個屬性引用相同的類型

我發現一些頁面解釋瞭如何爲同一類型的2個單個對象執行此操作。但不適用於列表和單個對象。
How can I set up two navigation properties of the same type in Entity Framework

這裏是我的代碼:

using System.Collections.Generic; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 

namespace DataAccess 
{ 
    public class SimpleVideo 
    { 
     public string Name { get; set; } 
     public int SimpleVideoId { get; set; } 
     public virtual List<Sub> Subs { get; set; } 
     public Sub MainSub { get; set; } 
    } 

    public class Sub 
    { 
     public int SubId { get; set; } 
     public string Language { get; set; } 
    } 

    class VideoContext : DbContext 
    { 
     public DbSet<SimpleVideo> SimpleVideos { get; set; } 
     public DbSet<Sub> Subs { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      Database.SetInitializer(new DropCreateDatabaseAlways<VideoContext>()); 

      modelBuilder.Entity<SimpleVideo>().HasMany(v => v.Subs).WithOptional().WillCascadeOnDelete(true); 
      modelBuilder.Entity<SimpleVideo>().HasOptional(v => v.MainSub).WithRequired().WillCascadeOnDelete(true); 

      base.OnModelCreating(modelBuilder); 
     } 
    } 
} 

我不能得到這個工作。 當我只啓用了一個modelBuilder行,它的工作原理。 我如何讓他們兩人同時工作?

System.Data.SqlServerCe.SqlCeException was unhandled 
    HResult=-2147467259 
    Message=The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_dbo.Subs_dbo.SimpleVideos_SubId ] 
    Source=SQL Server Compact ADO.NET Data Provider 
    ErrorCode=-2147467259 
    NativeError=25083 
    StackTrace: 
     at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) 
     at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor) 
     at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) 
     at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() 
     at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) 
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) 
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) 
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements) 
     at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto) 
     at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) 
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) 
     at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) 
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) 
     at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) 
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() 
     at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) 
     at System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext) 
     at System.Data.Entity.Database.Create(Boolean skipExistsCheck) 
     at System.Data.Entity.Database.Create() 
     at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context) 
     at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c) 
     at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6() 
     at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) 
     at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() 
     at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) 
     at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) 
     at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) 
     at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() 
     at System.Data.Entity.Internal.InternalContext.Initialize() 
     at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) 
     at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() 
     at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() 
     at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) 
     at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) 
     at System.Data.Entity.DbSet`1.Add(TEntity entity) 
     at DataAccess.Program.Main() in c:\MMProject\TestProjects\DataAccess\Program.cs:line 63 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+0

模型生成器行是什麼? – 2013-04-20 20:43:23

回答

0

看看這個帖子我的完全相同的結構處理(據我可以告訴)...
Entity Framework One-to-Many with Default

我沒有測試這一點 - 所以才替換名稱 - 這應該工作,我認爲...

modelBuilder.Entity<SimpleVideo>() 
    .HasOptional(x => x.MainSub) 
    .WithOptionalPrincipal() // x => x.DefaultForEntity) 
    .WillCascadeOnDelete(true); 

你不需要另一個關係 - 級聯是默認情況下。

+1

首先,感謝您的回覆。這裏使用的類只是爲了測試場景。這兩個屬性是完全獨立的,MainSub屬性中的對象不存在於列表Subs中。所以MainSub不是「默認」。 – 2013-04-22 07:32:18

+0

它的'語義'無關緊要 - 你的數據看起來就像那樣。 – NSGaga 2013-04-22 15:20:20

相關問題