EF: How to use DB across multiple projects in the same solution? - c#

I have three projects:
one containing all the models
one containing the controllers for my frontend
one containing the controllers for my customers frontend
I created a database for project (1) using entity framework (enable-migrations, add-migration, update-database).
Then I startet project (2), to create a new item and save this item in the previously created database.
However, instead of using said database, it instead created a new one (identical to the one from (1)) and saved the item there.
When starting project (3) I'm fairly sure the same thing would happen, creating a third identical database. However since (3) can't create new items, only read them, nothing happens instead.
Now that's obviously not what I want, since I need to use the items created from (2) in (3) (and ideally have them saved in the database created in (1)), so how can I achieve, that every project uses the same database?
The connectionStrings across all three projects are identical:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-db.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The second project has a class 'Startup.Auth.cs', which the other ones don't. Since I'm not sure if it has something to with it, here's part of it:
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(MyContext2.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
And here's the part of project (1) where I'm creating the context. The other projects don't have this:
public class MyContext2 : IdentityDbContext<ApplicationUser>
{
public MyContext2()
: base("DefaultConnection")
{
}
public DbSet<Items> items { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
Intellisense works fine, it automatically fills out db.items as it's suppost to, so the references are set. I'm callng the database via private MyContext2 db = new Context().
I can't get it working no matter what I try, any help is much appreciated!

Related

Replacing DbContext in Microsoft MVC Individual User Accounts Template

I just created my first MVC Website.
To avoid having to program the controller myself, I am using the Individual User Accounts template from Microsoft.
I know, that this template uses the Entity Framework to create an express database to persist the user/account data.
Since I already have a database, which I want to use, I want to change the
template so it uses the DbContext for said database.
I was able to change the connectionString, so that the tables of the template got created in my database. But I don't want it to create it's own tables but use my already created tables.
Is there any easy way to achieve this?
Or should I just write the whole account/user controller from scratch myself?
// This is an example of DbContext class it implements DbContext
// If you do not use constructor(s) then the expectation by entity framework
// will be that your name of your connectionstring in web.config
// or app.config is name name as your class so e.g. "YourContext",
// otherwise "Name="YourConnectionString"
public class YourContext : DbContext
{
// constructor as you wish /want
public YourContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{ }
// critical mapping
public DbSet<someModel> someModel { get; set; }
// critical overide
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// critical key to NOT let your database get dropped or created etc...
Database.SetInitializer<YourContext>(null);
// This is an example of mapping model to table
// and also showing use of a schema ( dbo or another )
modelBuilder.Entity<someModel>().ToTable("someTable", schemaName: "dbo");
}
}

Moving Entity framework to another project from MVC causes re-migration

I currently have an asp.net MVC 4 application which contains Entity framework 6 Code First models, DbContext and Migrations. In an attempt to separate this from my web application so I can re-use these database classes in another project I have moved all related Entity Framework classes to their own project.
However now when I run the solution it thinks my model has changed and attempts to run all my migrations once more. The problem appears to be in my use of SetInitializer as if I comment out this line I can run the web application as per normal.
public static class DatabaseConfig
{
public static void Initialize()
{
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<G5DataContext, Configuration>());
// make sure the database is created before SimpleMembership is initialised
using (var db = new G5DataContext())
db.Database.Initialize(true);
}
}
This wasn't a problem until I've tried to move all the Entity Framework classes. Is this not possible, or have I done something fundamentally wrong?
At startup, EF6 queries exiting migrations in your database, as stored in the __MigrationHistory table. Part of this table is a context key, which includes the namespace of the entities.
If you move everything to a new namespace, EF6 doesn't recognize any of the previously run migrations, and tries to rebuild the database.
A quick solution is to run a script to rename the context key in the __MigrationHistory table to your new namespace. From http://jameschambers.com/2014/02/changing-the-namespace-with-entity-framework-6-0-code-first-databases/ :
UPDATE [dbo].[__MigrationHistory]
SET [ContextKey] = 'New_Namespace.Migrations.Configuration'
WHERE [ContextKey] = 'Old_Namespace.Migrations.Configuration'
Would also like to add that you should remember to change the ContextKey property in your Configuration class. I did the above but it was still trying to create a new database. Here's an example:
Before:
internal sealed class Configuration : DbMigrationsConfiguration<PricedNotesContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey = "Synapse.DAL.PricedNotesContext";
}
protected override void Seed(PricedNotesContext context)
{
}
}
After:
internal sealed class Configuration : DbMigrationsConfiguration<PricedNotesContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey = "SynapseDomain.DAL.PricedNotesContext";
}
protected override void Seed(PricedNotesContext context)
{
}
}
Hope this helps anyone who is stuck on this. It's a shame that it shouldn't be easier...

Does IdentityDbContext always need a DefaultConnection

I was working at an asp.net MVC project with the IdentityDbContext.
The code for the context:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("ApplicationDb")
{
}
}
And in my web.config a connectionstring named after the context:
<add name="ApplicationDb" connectionString="Data Source="..." providerName="System.Data.SqlClient" />
Strange thing is when I call the Update-Database command to create the database with Entity Framework, my database is created. So far so good.
But: the authorisation code from Owin is also creating a second upon running the application. This one is named DefaultConnection and is copy from the other one.
So my question: does Identity Framework always need a connection string named "DefaultConnection", even if you point the context to another connectionstring?
In the end I managed to solve this by adding the DefaultConnection connectionstring in web.config so I end up with two connectionstring:
ApplicationDb
DefaultConnection
Is this really the way to go? Because if that's the case it doesn't make much sense to put a custom connectionstring name in the base constructor?!
Btw, I also tried the context like so:
public ApplicationDbContext()
{
}
Which in theory should effectively do the same. But still DefaultConnection is created upon running the app. Doesn't make sense to me.

ASP.NET MVC 5 Identity with Model First

I'm developing a web application and I would like to implement Identity. I've done a couple of tests and it's pretty easy to implement when using a Code First approach.
My issue is that I can't get it to work with a Model First approach. I would like to define my models in the .edmx file, and generate my database using the "Generate database from model" option.
Here's what I've done:
I started from the default ASP .NET Web Application template, including the "Individual User Account" authentication. Then I registered a user in order for entity framework to generate the needed tables in the default database.
Then I added the "ADO .NET Entity Data Model" to my project, and chose the "EF Designer from Database" option. It has been generated successfully with the existing tables created by Identity. I changed the connection string in the IdentityModels:
public ApplicationDbContext()
: base("MyConnectionStringName", throwIfV1Schema: false)
{
}
But after this, when I try to register a user, I get the following error:
"Unable to load the specified metadata resource."
Previously, I also had an error: "The entity type ApplicationUser is not part of the model for the current context."
Is it actually possible to use Identity with a Model First approach ? If yes, what am I doing wrong ?
I said I'll post something in order to explain how I managed to get this working. I struggled to find some documentation and tutorials about using Identity with a Model First approach, so here it comes.
Firstly, I found this genius article from Ben Foster: http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1
This article discuss about how to implement an "Identity Stripped Bare". Reading it and following the steps allowed me understand how to use identity from scratch (you can see at the beggining that he generates an MVC 5 project with no authentication system).
If you want the code of this "Naked Identity", here it is: https://github.com/benfoster/NakedIdentity
The first step was to generate the database. In the Naked Identity code, the v1.0 of Identity is used. Some things are different (a couple table properties for example), but it stays mostly identical.
To create a database usable by Identity, I simply ran the template MVC 5 project, registered a user for the table to be created with the Code First approach, and then copied those tables needed by Identity in my empty database.
Once this was done, I generated my .edmx using the freshly created database. A connection string is added to the web.config. A connection string may already exist as "DefaultConnection". You need to keep this one as well.
Basically, those 2 connection strings are needed because, Identity is gonna use the default one, and the .edmx is gonna use the other one. It is not possible to use the same one for both as the .edmx connection string needs metadata, that are not supported by identity. Thus, those 2 connection strings are different, but I modified them so they can refer to the same database.
<connectionStrings>
<add name="DefaultConnectionString" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=DatabaseName; Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="EntitiesConnectionString" connectionString="metadata=res://*/Models.WebAppModel.csdl|res://*/Models.WebAppModel.ssdl|res://*/Models.WebAppModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\v11.0;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
The first connection string is the one you're gonna use with entity. So here is how the IdentityDbContext is initialized:
``
public class AppDbContext : IdentityDbContext
{
public AppDbContext() : base("QualityWebAppDbEntitiesDefault", throwIfV1Schema: false)
{
}
public static AppDbContext Create()
{
return new AppDbContext();
}
}
``
(you can find the AppUser definition in the NakedIdentity sources)
And for my .edmx, the initialization looks like this:
public partial class QualityWebAppDbEntities : DbContext
{
public QualityWebAppDbEntities()
: base("name=QualityWebAppDbEntities")
{
}
}
Hope this will help people that want to use a Model First approach with Identity.

Entity Framework Code First without app.config

I hope somebody is able to help me, because it seems I'm totally stuck.
For upcoming projects in our company we'd like to use Entity Framework 5 with an code first approach. I played around a little while and everytime I try to use EF with our existing libraries, I fail because it seems EF heavily relies on an existing app.config.
In our company, we have an inhouse database library that allows us to connect to various data sources and database technologies taking the advantages of MEF (managed extensibility framework) for database providers. I just have to pass some database settings, such as host (or file), catalog, user credentials and a database provider name, the library looks for the appropriate plugin and returns me a custom connection string or IDbConnection.
We'd like to use this library together with EF because it allows us to be flexible about which database we use also change the database at runtime.
So. I saw that a typical DbContext object takes no parameters in the constructor. It automatically looks for the appropriate connection string in app.config. We don't like such things so I changed the default constructor to take a DbConnection object that get's passed to the DbContext base class. No deal.
Problems occur when the code first model changes. EF automatically notices this and looks for migration classes / configuration. But: A typical migration class requires a default parameterless constructor for the context! What a pity!
So we build our own migration class using the IDbContextFactory interface. But again, it seems that also this IDbContextFactory needs a parameterless constructor, otherwise I'm not able to add migrations or update the database.
Further, I made my own data migration configurator where I pass the context, also the target database. Problem is here: It doesn't find any migration classes, no matter what I try.
I'm completely stuck because it seems the only way to use EF is when connection strings are saved in app.config. And this is stupid because we need to change database connections at runtime, and app.config is read-only for default users!
How to solve this?
The answer is provided here
https://stackoverflow.com/a/15919627/941240
The trick is to slightly modify the default MigrateDatabaseToLatestVersion initializer so that:
the database is always initialized ...
... using the connection string from current context
The DbMigrator will still create a new data context but will copy the connection string from yours context according to the initializer. I was even able to shorten the code.
And here it goes:
public class MasterDetailContext : DbContext
{
public DbSet<Detail> Detail { get; set; }
public DbSet<Master> Master { get; set; }
// this one is used by DbMigrator - I am NOT going to use it in my code
public MasterDetailContext()
{
Database.Initialize( true );
}
// rather - I am going to use this, I want dynamic connection strings
public MasterDetailContext( string ConnectionString ) : base( ConnectionString )
{
Database.SetInitializer( new CustomInitializer() );
Database.Initialize( true );
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class CustomInitializer : IDatabaseInitializer<MasterDetailContext>
{
#region IDatabaseInitializer<MasterDetailContext> Members
// fix the problem with MigrateDatabaseToLatestVersion
// by copying the connection string FROM the context
public void InitializeDatabase( MasterDetailContext context )
{
Configuration cfg = new Configuration(); // migration configuration class
cfg.TargetDatabase = new DbConnectionInfo( context.Database.Connection.ConnectionString, "System.Data.SqlClient" );
DbMigrator dbMigrator = new DbMigrator( cfg );
// this will call the parameterless constructor of the datacontext
// but the connection string from above will be then set on in
dbMigrator.Update();
}
#endregion
}
Client code:
static void Main( string[] args )
{
using ( MasterDetailContext ctx = new MasterDetailContext( #"Database=ConsoleApplication801;Server=.\SQL2012;Integrated Security=true" ) )
{
}
using ( MasterDetailContext ctx = new MasterDetailContext( #"Database=ConsoleApplication802;Server=.\SQL2012;Integrated Security=true" ) )
{
}
}
Running this will cause the two databases to be created and migrated according to the migration configuration.
It needs a parameterless constructor in order to invoke it. What you could do is provide your default DbConntectionFactory in the empty constructor, something like:
public DbContext()
{
IDbContextFactory defaultFactory; //initialize your default here
DbContext(defaultFactory);
}
public DbContext(IDbContextFactory factory)
{
}

Categories

Resources