none
EDMX and DbContext? Database First

    Question

  • Rowan I am trying to whip up a quick app with an existing DB.

    Is there a path to create an EDMX file and then create my DbContext with DbSets so that I can continue to work with all othe greatness of CTP4 stuff?

    Model:

    	public class IssueTrackerDB : DbContext
    {
    public DbSet<ITIssue> ITIssues { getset; }
    Controller:
    			IssueTrackerDB DB = new IssueTrackerDB();
    var itIssues = DB.ITIssues;
    I have tried this and am getting this error

    base {System.Data.EntityException} = {"Schema specified is not valid. Errors: \r\nThe types in the assembly 'IssueTracker.MVC3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be loaded because the assembly contains the EdmSchemaAttribute, and the closure of types is being loaded by name.  Loading by both name and attribute is not allowed.


    Thanks, Terrence

     

     

    Wednesday, November 10, 2010 4:28 PM

Answers

  • Hi Terrence,

    It is possible but a little messy in CTP4;

    class Program
    {
      static void Main(string[] args)
      {
        var builder = new ModelBuilder();
        builder.RegisterEdmx("<path to EDMX file>");
        var model = builder.CreateModel();
    
        using(var db = new IssueTrackerDB(model))
        {
          // Data access code
        }
    
      }
    }
    
    public class IssueTrackerDB : DbContext
    {
      public IssueTrackerDB(DbModel model)
        : base(model)
      { }
    
      public DbSet<ITIssue> ITIssues { get; set; }
    }

    This will be a lot cleaner in CTP5, in fact we will ship T4 templates that will generate the context for you off the EDMX file.

    ~Rowan

    Wednesday, November 17, 2010 7:02 PM

All replies

  • Hi Terrence,

    It is possible but a little messy in CTP4;

    class Program
    {
      static void Main(string[] args)
      {
        var builder = new ModelBuilder();
        builder.RegisterEdmx("<path to EDMX file>");
        var model = builder.CreateModel();
    
        using(var db = new IssueTrackerDB(model))
        {
          // Data access code
        }
    
      }
    }
    
    public class IssueTrackerDB : DbContext
    {
      public IssueTrackerDB(DbModel model)
        : base(model)
      { }
    
      public DbSet<ITIssue> ITIssues { get; set; }
    }

    This will be a lot cleaner in CTP5, in fact we will ship T4 templates that will generate the context for you off the EDMX file.

    ~Rowan

    Wednesday, November 17, 2010 7:02 PM
  • Thanks Rowan, can't wait until the CTP5.  I noticed in one of the PDC demos that the guy downloaded it with NuGet. 

     


    Thanks, Terrence
    Thursday, November 18, 2010 1:25 PM
  • Hi Terrence,

    Yes CTP4 is available via NuGet and CTP5 will be too :)

    ~Rowan

    Thursday, November 18, 2010 7:22 PM