ASP.net MVC, I want one database, not multiple - asp.net-mvc

I am following the guide on the asp.net site for learning asp.net mvc4.
Link : www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/
Thing is, I want to put my movies table in the Default connection. Because I want all of the data to be in ONE database and not two.
I mean. I am confused as to why I can't just have one database, with separate tables. Surely multiple databases will introduce latency, and also scalability issues to my project.
How can I get around this?

That Default connection points to your local db and is what is used for the forms authentication stuff. That db has all of the asp.net role provider schema stuff in it and since it's an "internet" project this is where all the login stuff goes. if you want to have your entities and models hook into that same database when you add your entity framework model point it at that db and your good to go.
After a little further investigation:
It looks like http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model is where you define your connection string for your entities. I noticed AttachDbFilename=|DataDirectory|\Movies.mdf in there, and after verifying my thoughts http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/f21c0728-935d-492a-baaf-ff2704e3683b/ it looks like that is what is spinning up a second database in the app's data directory called movies.mdf. If you want to keep your 1 default database, change the connection string information in your MovieDBContext string to that of the default connection and and it should create your new movie structure within that same database.

Related

Is there a simple way to add a model in ASP.NET Core MVC via the GUI, besides the package manager console?

At work, I use ASP.NET MVC (not .NET Core) with Entity Framework and SQL Server. However, I wanted to practice a bit with ASP.NET Core MVC and some other SQL Server stuff (like experimenting with SSIS) at home, so I set up a small dev environment for me to work in. I created my database and tables, populated it with information, and created the default template you get with ASP.NET Core MVC. No issues so far!
But then, when I went to add a model into my project, I had no option to add an ADO.NET Entity Data Model like I do at work. After googling around a bit, I saw a mention that you could not create these kinds of models in .NET Core applications: https://learn.microsoft.com/en-us/answers/questions/357012/can39t-find-adonet-entity-data-model-missing-visua.html. I did some more googling, and it seems like just about every single thing I'm finding online is that you have to use package manager console, and type it all out.
Is this really the only way? Surely there has to be something better... It was very nice in ASP.NET MVC when I could add the model, and then it would take me through a wizard to get everything set up. I could create a new connection string, test the connection to the database, select which tables/views I wanted to add to the model, and I was all set! It was just as nice being able to go into my model and easily update the model with new tables, columns, or anything else I needed.
I get that code-first solutions are more mainstream these days, but I want to stick with the database first approach. Is there a simple, user-friendly (non package-manager console) approach for me to add models like I used to, but still get the benefits of .NET Core? Any good tutorials out there to get me on the right track (that aren't code first)? I appreciate anyone who can point me in the right direction!

Use existing aspnet membership schema with new ASP.NET MVC database first

I'm looking for some guidance here... I have an old ASP.NET webforms site with an existing database and SQL Server membership, and I'm creating a new interface using ASP.NET MVC and Entity Framework with a database-first approach.
So I have tables as shown below:
My problem is, I'm unsure how to make use of these old membership tables for authentication in the new ASP.NET MVC application.
I don't mind whether I migrate the user/roles data to ASP.NET Identity or continue using the old membership database but I'm not sure what the best approach is. I think I would prefer to migrate the data, as I am more familiar with Identity.
What I've tried...
While searching for solutions I have come across this: Migrating an Existing Website from SQL Membership to ASP.NET Identity several times, however this (and most of what I've found so far) is talking about upgrading an existing webforms site to use asp.net identity. I have tried using parts of it to migrate the Membership data to asp.net identity, but of course the remaining instructions don't fit my scenario.
So here's where I'm at now.
I have used the SQL script in the walkthrough linked above to migrate membership data to asp.net identity tables.
I have set up SQLPasswordHasher as described in the walkthrough, since I figure I'll need that function to use existing logins.
I imported those tables to the database that contains the website data, in hopes that I'd only have to deal with a single connection string.
I modified ApplicationDbContext : base... to use that database (it was previously using DefaultConnection (LocalDb)
So now I have these tables populated with data:
Aaand I'm stuck.
It's entirely possible that I'm missing something basic, but anyway if I try to login or register a new user in it's current state I get the error:
The entity type ApplicationUser is not part of the model for the current context.
on the line:
var result = await UserManager.CreateAsync(user, model.Password);`
The only solutions I've been able to find for that error have been on code-first projects: something to do with migrations, which I'm not using (I did try it but received an error on enable-migration "Creating a DbModelBuilder or writing the EDMX from a DbContext created using database-first or model-first is not supported.")
UPDATE
Ok so I came across an answer regarding the The entity type ApplicationUser is not part of the model for the current context. error, apparently using the connection string generated by entity framework can be problematic, so as advised, I made a 'normal' connection string and this seems to be getting closer!
UPDATE 2
I think I have it working, will run some tests though before updating with my solution.
Please follow the below step to do this.
Create a new ASP.NET MVC Project with user authentication.
Now right click on Model folder and add a new item "ADO.NET Entity Data model" enter your model name and select ok now select Database first approach and provide your database name and connection and connection name to be saved in web.config file.
Now go to your web.config file and you will see two connection are defined in your connection string and each connection point to different database , one for IDENTITY and other for which you have added just now.
for eg:
you need to change the data source of the both connection to be same.
So whenever you need to access user info you need to use ApplicationDbContext and for others you can use you 'CustomConn' context which you have defined earlier while adding new model.
Hope this will help you !

Workflow with MVC 4 - EF 5 - SimpleMembershipProvider

So I want to build an application with MVC 4 and Entity Framework 5. I've build simple applications before, but now I need some security around my current effort... I have some confusion / questions that I was hoping someone could answer;
First... Using the MVC 4 Internet Application Template it implements SimpleMembershipProvider. I have read every primary article about modification, implementation... However, this uses a Code-First implementation...
Problem: I have an existing database that I would like to import the scheme for to an EDMX database first approach... How do I implement the MVC 4 Simple membership provider when my database ties tightly and directly into the user table (userid)?... I know I can use my own user table as long as i designate the userid and username fields as documented... Will this affect the provider, or the existing "AccountController" code? Will these need to be modified?
Second, what I am looking for is a workflow with this architecture... I am "old school" mostly database first approach... My project is a huge WIP (work in progress). I have a foundation, but will need to expand as needed... Can someone provide some insight into database first vs other approaches when there will be quite a bit of change management occurring?
you can still use Code First to map to an existing database. You may need to explicitly map properties to table columns because the mappings do not follow the default conventions, but that doesn't prevent you from using Code First.
When transitioning from DB first to another mindset. Focus on how the objects interact with each other. then, at some point you will save the state of the objects after they interacted. This is where the ORM comes into play. detects changes and executes the necessary SQL statements to persist the current state of the objects.
Think of the database as just another storage container. In theory it could be replaced by another persistent storage mechanism (document db, file, persistent hash table, in memory list, etc.). In reality it's not that simple, but the idea of treating the DB as just a simple storage container helps to break away from the monolithic database concept that is/was ingrained into most devs.
But don't loose perspective of the design either. If it's a simple forms-over-data app where you will be adding features in the future than keep the design simple. than don't try to totally abstract the DB away. you know it's there and the relationship to the UI is almost 1:1, so take advantage of that.
In it's simplest form separation of concerns can be achieved by using the MVC controller to manage the interaction between the model (mapped to the DB via ORM) and the view (razor templates) my personal preference is to keep ORM out of the views so I typically query the database, map the domain model to a viewmodel and then pass the viewmodel to the view.
Again if it's a simple application and screens map directly to the database than viewmodel are probably overkill.

Datawarehousing with ASP.NET MVC

On one server there are more than 20 databases with identical structure but different data. I need to collect some of the data (the same queries) from all databases and store in new database which is located on another server. I decided to use ASP.NET MVC 2 but it doesn't seem logical to use more than 20 "LINQ to SQL Classes" (.dbml) files because the structure is the same for all databases and it's repeating if I use so many of these files. Is there a simple way to use one .dbml file (for remote databases) but change only connection string?
I agree that you really wouldn't want to use MVC as that is a web framework and has nothing to do with moving data around.
You can also look into using an ETL tool to accomplish this task. I have used RhinoETL in the past successfully to accomplish something similar.
There are also multiple posts on this site discussing ETL tools. For example, check the following link - https://stackoverflow.com/questions/51198/what-etl-tool-do-you-use
According to this, you can pass in a connection string with the dataContext constructor. So theoretically, you should be able to have one dbml file, but you can instantiate multiple instances of your data context, each with a different database connection string specified. Each context should then point to their respective database and allow you to work with multiple databases.
Why do you want to use ASP.NET MVC at all? ASP.NET is for web UI, not data warehousing (except when you need to display cubes). Looks like you use SQL Server. If that is true you can utilize Integration Services (ex DTS) to do the job.

EF4 and ASP.Net MVC to Test and Develop without mapping to an underlying database (until later)

I want to develop an ASP.Net MVC application with EF4 Model First design and only generate the actual database much later, ideally at the end of the project.
Based on some of the ideas here:
http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/
I want to create something like an InMemoryObjectContext for testing and development and use IOC to switch to the SQL Server implamentation (EF generated) for UAT and production.
Is this wise?
Is it possible? If so, does anyone have any suggestions?
Does EF always need an underlying database in order to track changes, commit etc?
I've tried creating a model first but as soon as I add properties I get the following errors:
Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer Model1Container.
and the warning:
Running transformation: Please overwrite the replacement token '$edmxInputFile$' with the actual name of the .edmx file you would like to generate from.
The error doesn't stop the application running but worries me. I'm very very new to EF so I apologize if this is way off the mark or a dumb question. I'm hoping to get some good advice while I sit for the next few days and watch videos and read articles.
Thanks
Davy
At the very least you need mapping information "filled in". You can fill these fields with nonsense if you don't want to work against the underlying database.
If your doing Model first, right click on the designer canvas and select, "Generate Database from Model". This will automatically create convention based mappings for you without defining tables and columns. You don't even need a valid db connection to do this.

Resources