New Posts New Posts RSS Feed: CF Seed Fail
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

CF Seed Fail

 Post Reply Post Reply
Author
esaulsberry View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Oct-2011
Location: Atlanta
Posts: 9
Post Options Post Options   Quote esaulsberry Quote  Post ReplyReply Direct Link To This Post Topic: CF Seed Fail
    Posted: 06-Jun-2012 at 12:24pm
Hello,
I've coded a few objects like this:

    public partial class AdmSite : EntityBase
    {
         /// <summary>Gets or sets the AdmSiteId. </summary>
        [Key]
        [DataMember]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("AdmSiteId")]
        [Bindable(trueBindingDirection.TwoWay)]
        [Editable(true)]
        public int AdmSiteId
        {
            get;
            set;
        }
. . . 
        [DataMember]
        [Bindable(false)]
        [Display(AutoGenerateField = false)]
        public IbEm.RelatedEntityList<PosTerminal> PosTerminals { get { return null; } }
. . . 
    }

    public partial class PosTerminal : EntityBase
    {
        #region Data Properties
        [Display(AutoGenerateField = false)]
        public int PosTerminalId { getset; }
 
        [Display(AutoGenerateField = false)]
        public int AdmSiteId { getset; }
 
        [IbVal.StringLengthVerifier(MaxValue = 100,
            IsRequired = true,
            ErrorMessageResourceName = "RequiredString_ValError")]
        public string ComputerName { getset; }
 
        [Display(AutoGenerateField = true,
            ResourceType = typeof(PosResources),
            ShortName = "PosTerminal_Description_ShortName",
            Description = "PosTerminal_Description_Description")]
        [IbVal.StringLengthVerifier(MaxValue = 100,
            IsRequired = true,
            ErrorMessageResourceName = "RequiredString_ValError")]
        public string Description { getset; }
        #endregion
 
        #region Navigation
        [DataMember]
        [Display(AutoGenerateField = false)]
        public AdmSite AdmSite { getset; }
        #endregion
	}
In the EF Code First Migrations Seed method, I'm creating data like this:
            AdmSite co =
                new AdmSite
                {
                    AdmSiteId = 1,
                    GMTOffset = -5,
                    IsActive = true,
                    Name = "Central Office",
                    CreatedDTS = DateTime.Now
                };
 
            context.AdmSites.AddOrUpdate(co);
 
            AdmSite siteHighSchool = new AdmSite
            {
                AdmSiteId = 2,
                GMTOffset = -5,
                IsActive = true,
                Name = "High School",
                AdmSiteIdParent = 1,
                CreatedDTS = DateTime.Now
            };
            context.AdmSites.AddOrUpdate(siteHighSchool);
            PosTerminal terminal1 = new PosTerminal
            {
                PosTerminalId = 1,
                ComputerName = "esaulsberry7",
                Description = "Elton's PC"
            };
            siteHighSchool.PosTerminals.Add(terminal1);
            context.PosTerminals.AddOrUpdate(terminal1);
 
            PosTerminal terminal2 = new PosTerminal
            {
                PosTerminalId = 2,
                ComputerName = "kgandy7",
                Description = "Kevin's Machine"
            };
            siteHighSchool.PosTerminals.Add(terminal2);
            context.PosTerminals.AddOrUpdate(terminal2);
This fails with:
Running Seed method.
System.Data.EntityException: Unable to set field/property PosTerminals on entity type Yargo.DataModel.AdmSite. See InnerException for details. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at IdeaBlade.EntityModel.NavigationEntityProperty.UpdateToNavigationProperty(EntityRelation relation)
   at IdeaBlade.EntityModel.NavigationEntityProperty.get_RelationLink()
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.GetEntityReference(EntityAspect aspect)
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.GetEntityReference(TInstance instance)
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.<.ctor>b__0(NavigationEntityPropertyGetInterceptorArgs`2 args)
   at IdeaBlade.Core.PropertyInterceptor`3.ExecuteActions(TArgs args)
   at IdeaBlade.Core.PropertyInterceptor`3.GetValue(TInstance instance, Object context)
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.GetValue(TInstance instance)
   at IdeaBlade.EntityModel.NavigationListEntityProperty`2.GetValue(Object instance)
   at IdeaBlade.Aop.ProvideEntityAspect.PropertyGetInterceptor(LocationInterceptionArgs args)
   at Yargo.DataModel.AdmSite.get_PosTerminals() in :line 0
   at lambda_method(Closure , Object )
   at System.Data.Objects.Internal.EntityProxyFactory.<>c__DisplayClass5.<CreateBaseGetter>b__4(Object entity)
   at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.GetNavigationPropertyValue(RelatedEnd relatedEnd)
   --- End of inner exception stack trace ---
   at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.GetNavigationPropertyValue(RelatedEnd relatedEnd)
   at System.Data.Objects.Internal.EntityWrapper`1.GetNavigationPropertyValue(RelatedEnd relatedEnd)
   at System.Data.Objects.EntityEntry.DetectChangesInRelationshipsOfSingleEntity()
   at System.Data.Objects.ObjectStateManager.DetectChangesInNavigationProperties(IList`1 entries)
   at System.Data.Objects.ObjectStateManager.DetectChanges()
   at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
   at System.Data.Entity.Internal.InternalContext.GetStateEntry(Object entity)
   at System.Data.Entity.Internal.InternalEntityEntry..ctor(InternalContext internalContext, Object entity)
   at System.Data.Entity.DbContext.Entry[TEntity](TEntity entity)
   at System.Data.Entity.Migrations.IDbSetExtensions.AddOrUpdate[TEntity](DbSet`1 set, IEnumerable`1 identifyingProperties, TEntity[] entities)
   at System.Data.Entity.Migrations.IDbSetExtensions.AddOrUpdate[TEntity](IDbSet`1 set, TEntity[] entities)
   at Yargo.DataModel.Migrations.Configuration.Seed(YargoDbContext context) in D:\Yargo\YargoShell\YargoDataModel\Migrations\Configuration.cs:line 113
   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()
Unable to set field/property PosTerminals on entity type Yargo.DataModel.AdmSite. See InnerException for details.
Any insight is appreciated.
BTW,
I've also tried:
            siteHighSchool.PosTerminals.Add(terminal1);
            context.AdmSites.AddOrUpdate(siteHighSchool);
            context.PosTerminals.AddOrUpdate(terminal1);
...
Same error
Back to Top
Walid View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Nov-2010
Posts: 161
Post Options Post Options   Quote Walid Quote  Post ReplyReply Direct Link To This Post Posted: 06-Jun-2012 at 4:13pm
I think your problem comes to the fact Code first migration is 100% Entity framework. The way we have to define the PostTerminals property for devforce isn't the same for Entity Framework (where you would have { get; set; } and not { get {return null;}})
So for code first migration, the navigation property PostTerminals always return null which explain your error when you try add an item to the list.
Back to Top
esaulsberry View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Oct-2011
Location: Atlanta
Posts: 9
Post Options Post Options   Quote esaulsberry Quote  Post ReplyReply Direct Link To This Post Posted: 07-Jun-2012 at 6:49am
Shouldn't PostScript have rewritten the navigation property by then though?  I have other entities with DF navigation properties that do work in the seed method. (I'm obviously at a loss to see what I've done differently here...)
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 07-Jun-2012 at 7:39pm
You should be able to seed the data given your existing model definition.  If the assembly was built correctly then PostSharp will have rewritten the navigation property and the Add method will work.  It's possible this isn't happening for some reason, or that the ibmmx file is either not present or invalid, so double check that PostSharp is referenced and the ibmmx generates correctly.
 
I've attached a simple small solution based on your model which does seed correctly.  Maybe it will help narrow down the problem.
 
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down