Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Entity Framework model from Visual Studio database project

I'm using EF5 with a Database-First model. And a database project in visual Visual Studio to maintain the Sql Server database schema of an application.

To update the EF model, I'm deploying the changes in a empty database...

Is it possible to generate and update a EF model from a Visual Studio (2012) database project?

UPDATE: Also generate it from a dacpac file is a not too bad option. Is it possible?

UPDATE: In the MS Build 2014 Conference, the ADO.NET team suggested that the future releases from EF, like EF7, will only work with the Code First approach.

Later, they clarify the name of the new approach should not be Code First, despite Code base modelling. Maybe is not exactly the same but as far as I read about it seems quite similar to me.

So I'm going to try @adam0101 solution. Any other proposed solution that ends with CodeFisrt pursues migrate from the SSDT project to EF's CodeFisrt project, and what I want is a smooth coexistence of both (maybe I'm a dreamer...).

like image 537
Alex Avatar asked Oct 15 '13 23:10

Alex


People also ask

How do you create a model class from a database?

You can add this table to an ASP.NET MVC project by following these steps: Right-click the App_Data folder in the Solution Explorer window and select the menu option Add, New Item. From the Add New Item dialog box, select SQL Server Database, give the database the name MoviesDB. mdf, and click the Add button.


1 Answers

I created the project SqlSharpener which should be able to do what you're asking.

For example, given these tables defined in an SSDT project:

CREATE TABLE [dbo].[Tasks]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [Name] VARCHAR(50) NOT NULL, 
    [Description] VARCHAR(1000) NOT NULL, 
    [TaskStatusId] INT NOT NULL, 
    [Created] DATETIME NOT NULL , 
    [CreatedBy] VARCHAR(50) NOT NULL, 
    [Updated] DATETIME NOT NULL, 
    [UpdatedBy] VARCHAR(50) NOT NULL, 
    CONSTRAINT [FK_Tasks_ToTaskStatus] FOREIGN KEY ([TaskStatusId]) REFERENCES [TaskStatus]([Id])
)

CREATE TABLE [dbo].[TaskStatus]
(
    [Id] INT NOT NULL PRIMARY KEY, 
    [Name] VARCHAR(50) NOT NULL
)

You can create a T4 template that will generate the entities as such:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace SimpleExample.EntityFrameworkCodeFirst
{
    public partial class TaskContext : DbContext
    {
        public TaskContext(): base()
        {
        }
        public DbSet<Tasks> Tasks { get; set; }
        public DbSet<TaskStatus> TaskStatus { get; set; }
    }


    [Table("Tasks")]
    public partial class Tasks
    {

        [Key]
        [Required]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Int32? Id { get; set; }

        [Required]
        [MaxLength(50)]
        public String Name { get; set; }

        [Required]
        [MaxLength(1000)]
        public String Description { get; set; }

        [Required]
        public Int32? TaskStatusId { get; set; }
        [ForeignKey("Id")]
        public virtual TaskStatus TaskStatus { get; set; }

        [Required]
        public DateTime? Created { get; set; }

        [Required]
        [MaxLength(50)]
        public String CreatedBy { get; set; }

        [Required]
        public DateTime? Updated { get; set; }

        [Required]
        [MaxLength(50)]
        public String UpdatedBy { get; set; }
    }

    [Table("TaskStatus")]
    public partial class TaskStatus
    {

        [Key]
        [Required]
        public Int32? Id { get; set; }
        public virtual ICollection<Tasks> Tasks { get; set; }

        [Required]
        [MaxLength(50)]
        public String Name { get; set; }
    }
}

There is a working example of this T4 template in the simple example solution. If there is a use case that SqlSharpener doesn't currently handle, feel free to add an issue and I'll see if I can add it in.

like image 193
adam0101 Avatar answered Oct 09 '22 22:10

adam0101