1

I am fairly new to Entity Framework and C#, so I apologize if my ability to describe the problem is minimal.

I have a .NET Web Forms project hosted on Azure that uses Entity Framework to handle all the database tables. I recently had an issue that I was debugging and I created test records, which I removed once the issue was solved using Microsoft SQL Server Management Studio. Unfortunately, I now get the following error:

"The model backing the 'RegSysContext' context has changed since the database was created. Consider using Code First Migrations to update the database"

The RegSysContext class can be seen below.

From what I understand, this is caused by the metadata that Entity Framework uses not matching the database anymore. The data in the database is essential to keep, otherwise I would just wipe it and forget about it.

How can I solve this issue on the hosted web app, and how can I edit the database in the future without causing this problem?

Thanks in advance!

public class RegSysContext : DbContext
{
    // Default Constructor for the context class
    public RegSysContext() : base("RegSys")
    {

    }

    // Properties of the context class
    public DbSet<CustomerAccountMembership> AccountMembershipRecords { get; set; }

    // For Organization Account functionality
    public DbSet<RegSysAccount> RegSysAccounts { get; set; }
    public DbSet<Subscription> Subscriptions { get; set; }
    public DbSet<SubscriptionRecord> SubscriptionRecords { get; set; }
    public DbSet<PaymentInfo> PaymentAccountInfoRecords { get; set; }

    // For Auction functionality
    public DbSet<Auction> auctions { get; set; }
    public DbSet<AuctionItem> auctionItems { get; set; }
    public DbSet<AuctionLot> auctionLots { get; set; }
    public DbSet<AuctionDonation> auctionDontations { get; set; }
    public DbSet<AuctionSale> auctionSales { get; set; }
    public DbSet<ItemInAuctionRecord> itemInAuctionRecords { get; set; }

    // For Event functionality
    public DbSet<Event> events { get; set; }
    public DbSet<EventParticipant> eventParticipants { get; set; }
    public DbSet<RegistrationPeriod> registrationPeriods { get; set; }
    public DbSet<RegType> regTypes { get; set; }
    public DbSet<RegPrice> regPrices { get; set; }
    public DbSet<EventCategory> EventCategories { get; set; }
    //public DbSet<RegistrationConfig> regConfigs { get; set; }
    //public DbSet<RegFormConfig> regFormConfigs { get; set; }

    // For Organization functionality
    public DbSet<Lodge> Lodges { get; set; }

    // For Participant functionality
    public DbSet<Participant> Participants { get; set; }
    public DbSet<Charge> Charges { get; set; }
    public DbSet<EventParticipantToParticipantLinker> EventParticipantLinks { get; set; }

    // For System functionality
    public DbSet<RegSysMessage> RegSysMessages { get; set; }
    public DbSet<Transaction> Transactions { get; set; }

    // For Trading Post functionality
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<CartItem> ShoppingCartItems { get; set; }
    public DbSet<ProductInventory> ProductInventories { get; set; }
    public DbSet<TradingPost> TradingPosts { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    public DbSet<ProductSale> ProductSales { get; set; }
    public DbSet<SpecialOffer> SpecialOffers { get; set; }
}
  • Have you tried any solutions? There's possibly one [here](http://stackoverflow.com/questions/25366807/the-model-backing-the-context-has-changed-since-the-database-was-created) or [here](http://stackoverflow.com/questions/28747581/entity-framework-telling-me-the-model-backing-the-context-has-changed) – Mark C. Aug 12 '16 at 17:51
  • @MarkC. I have been looking at the [MigrateDatabaseToLatestVersion Initializer](https://msdn.microsoft.com/en-us/data/jj591621.aspx#initializer) as it looked like it does what I need, though I am just trying to figure out how to use it right now. – Zackary Crosley Aug 12 '16 at 18:14
  • It seems like you haven't ran an `Update-Database` command then if you're doing Code First. Also, have you enabled migrations? – Mark C. Aug 12 '16 at 18:17
  • I definitely haven't run that command, as I am not familiar with it. What do you use to run the command, the Package Manager Console? How do you enable migrations? – Zackary Crosley Aug 12 '16 at 18:19
  • Are you following a tutorial or something? I'm trying to figure out what resources I can give you to help you – Mark C. Aug 12 '16 at 18:20
  • I followed a tutorial to make the project itself, but I have largely been making up the azure hosting side as I went along. The project has been up and running for a while however, it just broke once I edited the database manually. – Zackary Crosley Aug 12 '16 at 18:24
  • @MarcC. Is [this](http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application) what you are referring to? – Zackary Crosley Aug 12 '16 at 18:31
  • Mostly, yeah. Are you using Code First and does that stuff make sense? – Mark C. Aug 12 '16 at 18:34
  • I don't believe Code First Migrations was ever set up, but it sounds like that is what I need to do. – Zackary Crosley Aug 12 '16 at 18:40
  • Actually, Can you paste the code that is in your `RegSysContext` class? (we don't need to see the connection string) – Mark C. Aug 12 '16 at 18:41
  • Pasted in the original question, since it won't fit here – Zackary Crosley Aug 12 '16 at 18:45
  • Inside the constructor, put this code in and see if it works (as long as your Models of your tables match what's in the database, otherwise try an update-database script).. `Database.SetInitializer(null);` – Mark C. Aug 12 '16 at 18:47
  • That seems to have fixed it! What is that line of code doing? I assume for larger changes (adding or removing columns, for example) I still need to get Code First Migrations working for – Zackary Crosley Aug 12 '16 at 18:54
  • I think as long as your code matches your database, you should be fine (if you're making changes to the database manually) – Mark C. Aug 12 '16 at 18:55

1 Answers1

0

Add this line to your constructor inside your RegSysContext class :

Database.SetInitializer<RegSysContext>(null);

What this line of code does it tells Entity Framework whether or not to create the database. Passing null into the IDatabaseInitializer strategy parameter, you basically disable the initialization of the database altogether (since it already exists)

Mark C.
  • 6,332
  • 4
  • 35
  • 71