none
EF 5 Code-first custom discriminator column type 'int' throws Schema error on MaxLength Facet. RRS feed

  • Question

  • In Entity Framework 5.0.0 Code-First, I have a simple TPH with two concrete classes inheriting from an abstract base class.

    For the abstract/base class configuration I am attempting to specify a custom discriminator as so:

    Map<Derived1>(m => m.Requires("Discriminator").HasValue(1).HasColumnType("int"));

    Map<Derived2>(m => m.Requires("Discriminator").HasValue(1).HasColumnType("int"));

    This results in the following exception when initializing the database, if I change the column-type to 'char' and specify a HasMaxLength it works. It looks like the MaxLength facet is being applied even when the discriminator column is of INTEGER type.

    System.Data.MetadataException occurred
      HResult=-2146232007
      Message=Schema specified is not valid. Errors: 
    (597,6) : error 0063: MaxLength facet isn't allowed for properties of type int.
      Source=System.Data.Entity
      StackTrace:
           at System.Data.Metadata.Edm.StoreItemCollection.Loader.ThrowOnNonWarningErrors()
           at System.Data.Metadata.Edm.StoreItemCollection.Loader.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths)
           at System.Data.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerManifestToken, Memoizer`2& cachedCTypeFunction)
           at System.Data.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders)
           at System.Data.Entity.ModelConfiguration.Edm.Db.DbDatabaseMetadataExtensions.ToStoreItemCollection(DbDatabaseMetadata database)
           at System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping)
           at System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace..ctor(DbDatabaseMapping databaseMapping)
           at System.Data.Entity.Infrastructure.DbCompiledModel..ctor(DbModel model)
           at System.Data.Entity.Infrastructure.DbModel.Compile()
           at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
           at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
           at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
           at System.Data.Entity.Internal.InternalContext.Initialize()
           at System.Data.Entity.Database.Initialize(Boolean force)

    Wednesday, February 6, 2013 9:57 PM

Answers

  • I did manage to reproduce this. I realized that in my code I had not made the base class abstract.

    I have created a bug for this here: http://entityframework.codeplex.com/workitem/866

    It looks like the workaround for this is to call the column something other than "Descriminator". If I use a different name for the column then what you are trying appears to work fine.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Wednesday, February 13, 2013 11:15 PM
    Moderator

All replies

  • Hi Rince0000,

    Welcome to the MSDN forum.

    if for example we pass a value of type int to it then Code First not only use our desired values (i.e. 1 & 2) in the discriminator column but also changes the column type to be (INT, NOT NULL). You are not required to use HasColumnType method. Please use this instead:

    modelBuilder.Entity<Father>()
                .Map<Derived1>(m => m.Requires("Discriminator").HasValue(1))
                .Map<Derived2>(m => m.Requires("Discriminator").HasValue(2));

    You could refer to this page: http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 8, 2013 5:59 AM
  • Hi,

    I can't reproduce this on my machine, if it's happening then it is probably a bug in EF. You can work around it as Alexander has suggested, but EF probably shouldn't throw in this scenario. Can you provide a repro of it happening?


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Wednesday, February 13, 2013 10:54 PM
    Moderator
  • I did manage to reproduce this. I realized that in my code I had not made the base class abstract.

    I have created a bug for this here: http://entityframework.codeplex.com/workitem/866

    It looks like the workaround for this is to call the column something other than "Descriminator". If I use a different name for the column then what you are trying appears to work fine.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Wednesday, February 13, 2013 11:15 PM
    Moderator