0

I'm using Model-First approach and Oracle database.

Update2: Fixed Now

On including seed data, I'm getting this error "DeleteDatabase is not supported by the provider"

UPDATE1 If I change seed data type from
public class MySeedData : DropCreateDatabaseAlways<ToolContext> to
public class MySeedData : DropCreateDatabaseIfModelChanges<ToolContext>
this error is replaced by another error:

Exception Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns. DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility

Seed Data

 public class MySeedData : DropCreateDatabaseAlways<ToolContext>
    {
        protected override void Seed(ToolContext context)
        {
            base.Seed(context);

            var category = new List<CategoryValue>
            {
                new CategoryValue{Id=1, Name = "Associate"},
                new CategoryValue{Id =2, Name = "Professional"},
                new CategoryValue{Id=3, Name = "Master"},
                new CategoryValue{Id = 4, Name = "Product"},
                new CategoryValue{Id = 5, Name = "Portfolio"}
            };

            category.ForEach(cert => context.CategoryValues.Add(cert));
            context.SaveChanges();
        }
    }

Web.Config

 <connectionStrings>
    <add name="LMSPriorToolContext" 
           connectionString="metadata=res://*/Models.LMSPriorToolModel.csdl|res://*/Models.LMSPriorToolModel.ssdl|res://*/Models.LMSPriorToolModel.msl;
provider=Oracle.DataAccess.Client;
provider connection string=&quot;DATA SOURCE=DEV;PASSWORD=1234;PERSIST SECURITY INFO=True;USER ID=abc&quot;" 
providerName="System.Data.EntityClient" />
  </connectionStrings>

Application_Start()

Database.SetInitializer<ToolContext>(new SeedData());

Main.cs: EXCEPTION RAISED IN THIS FILE I know when you first try to access database, seed data method or scripts are executed.

 using (var dbContext = new ToolContext())
        {
            var items = dbContext.CategoryValues;

            foreach(CategoryValue category in **items**)  // **Getting error here**
            {
                Console.WriteLine(category.Name);
            }
        }

REFERENCES Seems like I'm missing something here as there is nothing related to Oracle or ODAC

References

Stack Trace

 at System.Data.Common.DbProviderServices.DbDeleteDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   at System.Data.Objects.ObjectContext.DeleteDatabase()
   at System.Data.Entity.Internal.DatabaseOperations.DeleteIfExists(ObjectContext objectContext)
   at System.Data.Entity.Database.Delete()
   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)

Please suggest what I'm missing here.

4

1 回答 1

0

Way of seeding data using DropCreateDatabaseAlways or DropCreateDatabaseIfModelChanges is not supported in Model-First approach.

Change seed data class to:

public class ToolSeedData : IDatabaseInitializer<ToolContext>
{
    public void InitializeDatabase(ToolContext context)
    {
        var category = new List<CategoryValue>
        {
            new CategoryValue{Id=1, Name = "Associate"},
            new CategoryValue{Id =2, Name = "Professional"},
            new CategoryValue{Id=3, Name = "Master"},
            new CategoryValue{Id = 4, Name = "Product"},
            new CategoryValue{Id = 5, Name = "Portfolio"}
        };

        category.ForEach(cert => context.CategoryValues.Add(cert));

        context.SaveChanges();
    }

Possible error if you don't use it:

  1. DeleteDatabase is not supported by the provider
  2. Exception Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns. DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility

Microsoft link Seeding database does not work

Hope this helps someone else.

于 2013-06-19T18:07:42.243 回答