New Posts New Posts RSS Feed: DF & EF 4.3 Migrations many-to-many seed data
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
esaulsberry View Drop Down

Joined: 26-Oct-2011
Location: Atlanta
Posts: 9
Post Options Post Options   Quote esaulsberry Quote  Post ReplyReply Direct Link To This Post Topic: DF & EF 4.3 Migrations many-to-many seed data
    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,

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:
                new Publication
                    PublicationId = 1,
                    Name = "pub 1",
                    Subscribers = new IdeaBlade.EntityModel.RelatedEntityList<Subscriber>()
                            new Subscriber
                                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?


Back to Top
DenisK View Drop Down

Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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 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 {..........};

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down