I have a Forms project that uses ASP.NET membership authentication to manage the users. I also have created model classes that use entity frameworks to populate a database. Currently, I have been planning on keeping the asp tables (membership and other records) in the same database as the tables containing the other application data.
When I run my program after changing some of the data model classes, entity frameworks recreates the entire database, and I've lost all my ASP.net tables.
What am I doing wrong here?
Without seeing, your code it is hard to say. Here is my wild guess.
You might have DropCreateDatabaseIfModelChanges. Basically, it drops the entire database when Code First classes change.
If so, you might want to replace it with NullDatabaseInitializer which prevents the schema from being altered.
Related
I have been asked to write a web site that will use an existing SQL Server database. The database was designed to work with another application, and so I can't make any potentially breaking changes to it.
Unfortunately, the database does not contain a single relational link, each table is standalone.
I know you can use EF commands to scaffold a database and create entity classes, but I would like the code to know the relationships that should exist between the tables.
I thought about scaffolding the database, then modifying the created classes to include the links, but I'm not sure if that would allow EF to load related entities. Also, as I will need to add some new tables to the database, I'm worried that EF will try and create those links when I do the migration.
Is there any way to do this?
I'm working on an ASP.NET project. I migrated my database named "youbay" with
reverse engineering. It worked. My database contains tables (picture, user, product...) but when I try to change something on the code and then update my database it with code first other tables are then created named youbay.picture,youbay.user...
What do you concretely mean with RE? Creating ASP.NET Models by guessing the mapped .NET types of the table scheme? Maybe your db-structures are a bit different from the ones EF would genereate itself, so that EF will see a conflict. Or EF keep track of the changes itself, so he isn't touching the tables because he won't recognize that he created them.
Whatever happened, it seems like your way of migrating was not very clean. You should tell EF use an existing database like explained here. This will prevent conflicts and also save work/time, because EF will automatically generate your models based of the database-scheme. So no RE is needed.
We want to progress towards being able to do continuous delivery of of our application into production. We currently deploy to azure and use table/blob storage and have a azure sql database, which we access with the entity.
As the database schema changes we want to be able to automatically apply the schema changes to the production database, but as this will happen whilst the application is live and the code changes are being deployed to many nodes at the same time we are not sure what the correct approach is.
After some reading it seems (and this makes sense) that the application needs to be tolerant of the 2 different database schema versions, so that it doesn't matter if its an old version of the code or a new version of the code which sees the database, however I'm not sure what the best way to approach handling this in the application is, using the entity framework.
Should we have versioned instances of the EF generated classes in the code which know how to access a specific version of the schema? What happens when the schema is updated and an old version of the code is running against the database?
Our entity framework classes are mapped to views in specific schemas in the db and nothing is mapped to the underlying tables, so potentially this could allow us to create v1 views which the old code uses and v2 views which the new code uses, but maintaining this feels like it would be a bit of a nightmare (its already enough of a pain simply maintaining the EF mappings to views rather than tables)
So what are best practices in this area? What do others do to solve this problem?
Whether you use EF or not, maintaining the code's ability to work with 2 consecutive versions of the database is a good (and perhaps the only viable) approach here.
Here are some ways we handle specific types of migrations:
When adding a column, we can typically just add the column (with a default constraint if non-nullable) and not worry about the code. EF will never issue a "SELECT *", so it will be able to continue to function properly while ignoring the new column. Similarly, adding a table is easy.
When removing a column or table, simply keep that column around 1 version longer than you would have otherwise.
For more complex migrations (e. g. completely changing the structure for a table or segment of the data model), deploy the new model alongside backwards-compatibility views (or tables with triggers to keep them in-sync), which will live as long as does the code that references them. As you say, this can a lot of work depending on the complexity of the migration, but it sounds like you are already well-positioned to do this because your EF entities point to views anyway. On the other hand, the benefit of this work is that you have more time to do the code migration. If you have a large codebase, this could be really beneficial in allowing you to migrate the data model to fit the needs of new features while still supporting old features without major code changes.
As a side-note, the difficulty of data migration often makes us push developing a finalized data model as far back as possible in the development schedule. With EF, you can write and test a lot of code before the data model is finalized (we use code-first to generate a sample SQLExpress database in a unit tests, even though our production database is not maintained by code-first). That way, we make fewer incremental changes to the production data model once a new feature is released.
I would like to know if it's possible to create multiple projects with the same Entity framework 5(here Code First) and if you have a complete example?
I also want to use MVP pattern in my solution.
I've saw one problem: the database is created with the first run but just for one projects not all.
I know that's not really accurate... but I just want to know if it's possible..
Thanks in advance!
In my last project we developed a database which captures the business domain and we also stored security-related tables in the same database but in a different schema. The domain model in visual studio included all the tables, but data access was separated into two projects. The first one was a data access layer that deals with essential business tables, and another one was a custom security authentication which had mappings related to security tables.
In addition to this, to add to complexity :-), we also added a number of DbContext-inherited classes (in our DAL) that capture the essence of the domain with which a user is working. For example, Human Resources context deals with HR-related tables, Logistics context deals with logistical tables, etc.
I would like to build a new web application using ASP.NET MVC3 and MongoDB. I've seen many examples online and even built some working code myself, but I am wondering about how I should set up my application. In the MVC examples which use Entity Framework, they place everything in a Models folder. I think I will do the same but where should I put my queries etc. Should I abstract them to a better location. I'm somewhat new to making C# applications and the .NET world, so some of the "ways" are not clear to me yet. Also, does creating the database object (where I tell it mongo's server address) each time I need it have performance impacts? Can I just connect once and then talk through that object? Does it really reconnect every time I perform that action?
Thanks!
Normally, in your Model, you have an object model representing your domain.
With MongoDB, this does not change. Your objects in your model will still have properties and behaviors.
What will change, is that instead of storing each object in a table in a relationnal model, you will be storing a graph of objects. Let's say you have an invoice. You will store the Invoice, with all the lines of the invoice as a single record. That's about it, not really more complicated than that.
First of all, don't use your domain objects (these that you supposed to save to RDBMS using Entity Framework or to MongoDB) directly in ASP.NET MVC views! Use viewmodels instead. Then you will have Models folder in ASP.NET MVC project and separate project for your domain.
I didn't work with MongoDB before, but I suppose the best way to have database object per http request. Here is discussion on stackoverflow and here is video from 10gen about their C# driver.