Skip to content

Instantly share code, notes, and snippets.

@morecchia
Last active March 16, 2016 19:33
Show Gist options
  • Save morecchia/4ca581698517fc7805fd to your computer and use it in GitHub Desktop.
Save morecchia/4ca581698517fc7805fd to your computer and use it in GitHub Desktop.
Use Entity Framework Code First to connect to a table in an existing database
using System.Data.Entity;
using MyProject.Models;
namespace MyProject.Database
{
public class MyDatabase : DbContext
{
// add a connection string in Web.config
// <add name="MyConnection" connectionString="Server=localhost;Database=MyDatabase;Trusted_Connection=true;" providerName="System.Data.SQLClient" />
public MyDatabase() : base("MyConnection") { }
public virtual DbSet<MyTable> ExistingTable { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyTable>().ToTable("MyTable", schemaName: "myschema");
base.OnModelCreating(modelBuilder);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment