Why does DataContext class create a new database? - asp.net

Guys I am a new bee to asp .net mvc 3.
In the MS Visual Web Developer Express 2010, I created a new project using Visual C# / Web / ASP .net MVC 3 Web App using a form authentication template. I deleted all the controller and view files and added my own. I added a new sample.mdf database in the App_Data folder and changed the connection string accordingly and created country model.
But when I create CRUD controller for the country model it asks me for the DataContext class. It does not accept empty and I have to mention DataContext class. And when I create a DataContext class, it creates a new database of same name as the DataContext class. It does not use the database that I specified in the web.config file.
I want Country model data to be added in the sample.mdf file and not in the context database.
Please help... Thank you.

You can pass in the connection string to use in context constructor. That way you can use any database you wish.

A Data context class is simply just a class that maps your model classes to tables in the database. In the web config the connection string name should be the same as your context class (datacontext.cs):
<add name="datacontext" connectionString="[connectionstringhere]" />
If the connection string pointed to a SQL CE database that doesn’t exist... When you run the application, EF detects that the database didn’t exist and automatically creates it from the models set in the data context class.

Related

Data context Class Missing in Identity

So I have a ASP Core 3.1 project and have managed to scaffold out via EF all the rest of my existing tables from my SQL server.
These all work fine and do as i need them to.
However when i go to add Identity to the project using my existing ASP User tables i cannot find the Data context class in the Scaffold menu.
I have already tried re-loading Identity, changing the base, and changing the startup file to look at the current database connection
Any help would be appreciated
I had the same problem. I was inheriting the superclass DbContext in my ApplicationDbContext. Since I was using Identity platform, changed it to IdentityDbContext. It fixed my issue.

AspNet Identity ApplicationDbContext on existing database

I have an existing database with my application tables and i am about to build a new version for my application using MVC5. I decided to use AspNet Identity framework as part of my application.
The visual studio template i used while creating the project added a file "IdentityModel.cs" and the class
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
}
So i can access the tables Users, Roles and other AspNet Identity tables using the code like:
var context = new ApplicationDbContext();
context.Users.ToList();
Because of the name Microsoft gave to the class "ApplicationDbContext" (and not: IdentityDbContext for example), i am wondering if that class should be used as "accessor" for all the rest existing tables that not related to AspNet Identity framework or not?
without the so generic name of the class "ApplicationDbContext" i would just use my entity framework project i just added to my solution to access the other tables of my application but i am wondering what is the "best practices". To use the same AspNet Identity ApplicationDbContext accessor (and how?) or to work with two Db accessors, one of the AspNet Identity tables and one for the entity framework i created for the rest of the tables (Db First).
it looks me more logic to use the same dbContext for all the tables, AspNet identity and all the rest of the tables i have in a separate EntityFramework edmx file. How can i use both of them in one dbContext?
AspNet Identity provide something like user manager, it should solve your problem. You don't need to operate on real tables, AspNet Identity should cover table structure and should allow to do high level operations. Your user model should inherit IdentityModel class, then you are able to build your own custom model.
It sounds like part of your issue is that while IdentityFramework is using code-first entity framework you are not using code-first for the rest of your application, instead using the older edmx designer approach. I would not try to mix the two into a single context.
Moreover, because ApplicationDbContext inherits from IdentityDbContext I would leave it alone. The base class implements things like OnModelCreating and ValidateEntity. If you wanted to use your other context for IdentityFramework you'd need to either have your other context inherit from IdentityDbContext--which is bad semantics, because it is not in fact just an identity context--or you'd need to implement these methods manually.
It is easy, however, to point the identity framework context to the same database as your other entity framework items. Just replace "DefaultConnection" in your ApplicationDbContext with whatever the name of the connection string you use for your other stuff is.

How do I update the default database in ASP.NET 2013?

So I created a website in ASP.NET 2013 and went through the login so the sample db would be created. Then I added some tables. Now I wanted to customize the AspNetUsers table with my own properties that I add to the ApplicationUser class. However when I add properties to the class and try to run the Update-Database command in the PackageManagerConsole, it fails with a "The parameter is incorrect" message. I thought this would be the way to go as I believe this is considered CodeFirst. This is an ASP.NET 4.5 VB Web Forms App.

Can I Send a custom class from asp .net 4.0 website to a WCF .net 4.0 service

I want to create a wrapper class called Report that contains dictionaries and lists and such in my website generated by 100-1000 fields from the website page, then send it to my WCF service.
I have tested my connected I can send a string or int and it works fine, I have not tried to send a custom class (im assuming the class would be attached BOTH to the website (ASP.NET 4.0) and the WCF service (.NET 4.0) in order to make this work.
The idea is to create the report class, fill it up with my data then send it to the wcf (the interface would have a [OperationContract] and then it would take a parameter of type Report, and then I could do something with that object on the WCF service side to pull data from dictionaries and lists from the object and send it to a database.
So I know right know my website and wcf are talking together as I have sent a string and returned an int it worked fine.
But I am wondering if I can pass over a class that I created (assuming both the website and wcf both have the same class file)?
As you need a confirmation by a totally trustworthy stranger on the internet, yes it works that way.
If you decide that listening to random people isn't going to help with the deadline, you may want to open a new WCF project in Visual Studio. It will generate a default service called Service1 that already does exactly what you want. It uses a user defined type in it's interface. Open a new ASP project and reference that service. The class will even be generated for you by the Visual Studio Wizard.
Hats off to the smart guys on SOF.com, but #Kairan - the class you made it in either Business Layer - this should have a parallel SOA layered Datacontract(aka class) and those fields which should have publicly available and entire class should be serializable ( which should not have fields which are not serializable- like sqlconnection.. . I know crude way of checking whether all classes are serializable or not of a ASp.net web application - Just set in web.config - and by navigating all pages, one can understand all the objects are serializable :)

How to consume an EF Model in a seperate project within a ASP.NET Solution correctly?

I am trying to find a way to have my Entity Framework model in a seperate project within my ASP.NET solution.
Currently I have my DataManager project (which contains my EF model and some classes) and a second project which contains all my web project files.
The problem that I have come across is that I have a database connection string in a App.Config file in my DataManager project and the same connection string in my Web.Config from my web project. I basically have a duplicate connection string.
Is there a way to only use only one connection string in my project (preferably from my web.config)?
My only concern is that when it comes to compiling my project I will not be able to change the connection string in the App.Config contained in my DataManager project.
I would be grateful if someone could help me in the correct way of having a seperate project to contain my EF model. Or suggestions on better ways.
Many Thanks!
In my project I have my EF in a domain layer and my UI in a seperate layer. The UI contains the webconfig file which in turn has the connection string that EF uses. So yes it's completely acheivable. My webconfig will superseed the app.config setting.
My suggestion it terms of layering your app would be to look at some DDD. I'm using EF4 in conjuction with MVC and the repository pattern, which seems an elegant fit. I'm not saying you need to use MVC, but it's certainly worth looking at having a domain layer for ef4.
This is the application I've used as my guide. Note this is based around mvc, but the domain aspect could be true of any application http://mvcsamples.codeplex.com/

Resources