Print Page | Close Window

DF & EF 4.3 Migrations many-to-many seed data

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3318
Printed Date: 16-Apr-2024 at 11:39am


Topic: DF & EF 4.3 Migrations many-to-many seed data
Posted By: esaulsberry
Subject: DF & EF 4.3 Migrations many-to-many seed data
Date Posted: 05-Mar-2012 at 9:22am

Is it possible to populate the association table for a many-to-many relationship in EF 4.3 Migration's Configuration.Seed method using DF?

I have a couple of classes with a many to many relationship defined as:

    public class Publication : EntityBase
    {
        public int PublicationId { getset; }
        public string Name { getset; }
 
        public RelatedEntityList<Subscriber> Subscribers { getset; }
    }

 

public class Subscriber : EntityBase { publicint SubscriberId { get; set; } publicstring Name { get; set; } publicRelatedEntityList<Publication> Publications { get; set; } }

The context includes:

        public DbSet<Publication> Publications { get; set; }
 
        public DbSet<Subscriber> Subscribers { get; set; }

This generated the tables I expect: Subscribers, Publications, and SubscriberPublications. In the configuration seed method, I can add some sample data:

context.Publications.AddOrUpdate(newPublication { PublicationId = 1, Name = "pub 1" }); context.Publications.AddOrUpdate(newPublication { PublicationId = 2, Name = "pub 2" }); context.Subscribers.AddOrUpdate(newSubscriber { SubscriberId = 1, Name = "Dude 1" }); context.Subscribers.AddOrUpdate(newSubscriber { SubscriberId = 2, Name = "Dude 2" });

What I don't seem to be able to do is

context.Publications.AddOrUpdate(newPublication {PublicationId = 1, Name = "pub 1", Subscribers.Add(somedude) });

and so my assocation table SubscriberPublications is empty.

The EF forum recommended way with plain EF,

context.Publications.AddOrUpdate(
new Publication {
PublicationId = 1,
Name = "pub 1",
Subscribers = new List<Subscriber>()
{ new Subscriber { SubscriberId = 1, Name = "Dude 1" },
new Subscriber { SubscriberId = 2, Name = "Dude 2" } } });
 
modified for my classes:
            context.Publications.AddOrUpdate(
                new Publication
                {
                    PublicationId = 1,
                    Name = "pub 1",
                    Subscribers = new IdeaBlade.EntityModel.RelatedEntityList<Subscriber>()
                        {
                            new Subscriber
                            { 
                                SubscriberId=1, 
                                Name="Dude 1"
                            }}
                });
 
 
results in this exception:
Running Seed method.
System.NotSupportedException: Sets on List Navigation properties are not supported
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.SetValue(Object instance, Object value)
   at IdeaBlade.Aop.ProvideEntityAspect.PropertySetInterceptor(LocationInterceptionArgs args)
   at DbModel.Models.Publication.set_Subscribers(RelatedEntityList`1 value) in :line 0
   at DbModel.Migrations.Configuration.Seed(DbModelContext context) in H:\Projects\2 - Research Projects\EFCF\SilverlightApplication2\DbModel\Migrations\Configuration.cs:line 42
   at System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.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(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Sets on List Navigation properties are not supported

Is there a way to populate the assocation table from the seed method with DF?

Thanks.




Replies:
Posted By: DenisK
Date Posted: 06-Mar-2012 at 6:03pm
Hi esaulsberry;

There are a couple of things that you need to change here.

1. RelatedEntityList is not supposed to have a setter. See http://drc.ideablade.com/xwiki/bin/view/Documentation/code-first-entity-classes#HPersistednavigationproperties - here for further discussion.

public RelatedEntityList<Publication> Publications { get { return null; } }
public RelatedEntityList<Subscriber> Subscribers { get { return null; } }

2. Because RelatedEntityList is not settable, you need to change your seed method as follows,

      var pub = new Publication {.........};
      var sub = new Subscriber {..........};
      pub.Subscribers.Add(sub);

      context.Publications.AddOrUpdate(pub);



Print Page | Close Window