We have an existing Firebird database which we wish to setup in an ASP.NET MVC project. By adding a ‘ADO.NET Entity Data Model’ item to the project and selecting the Firebird database in question, we are able to get automatically generated Models.edmx and Models.designer.cs files, which visually display the database tables and relationships.
In addition to this, as I understand it, when the application is built, behind the scenes it generates a Models.csdl, Models.ssdl and Models.msl.. but these files cannot be viewed or modified.
I don’t like so much control being taken away from me. It makes me nervous that I know so little about how my model is being mapped. I also don’t like having my Model classes automatically generated, as I want to add my own methods on to the model.
Can anyone tell me how I can build an ASP.NET MVC application without having my models automatically generated? I’d much prefer to map it out myself without any kind of wizard process, in order to gain a greater understanding.
Edit:
Now I’m getting somewhere. Here’s the code for one of my models and the context object.
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
public class COMPANY
{
[Key]
public string CODE { get; set; }
public string NAME { get; set; }
}
public class ModelContext : DbContext
{
public DbSet<COMPANY> Companies { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<COMPANY>().ToTable("COMPANY");
base.OnModelCreating(modelBuilder);
}
}
And connection string
<add name="ModelContext"
connectionString="data source=localhost;initial catalog=C:\SUPPORT.gdb;user id=SYSDBA;password=masterkey;character set="
providerName="FirebirdSql.Data.FirebirdClient" />
The connection string has to have the same name as the db context! (or be overridden somewhere else)
Now just generate the controller automatically and you’re good to go. If your database schema is anything like mine, you’re going to run into problems with the mappings and you’ll need to customize them.
The great thing about MVC is that you get to decide what happens with the implementation. Simply make a connection string in your root web.config file, make a model .cs that contains a mapping of your current database tables, then make a data access layer database context (entity framework makes this happen with a simple implementation). Then in your repository or controller you can access the database context, and through linq, access all your database data and manipulate it.