Last active
March 16, 2016 19:33
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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