Generate Entity Framework model from Visual Studio database project
Asked Answered
B

1

13

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...).

Bachelor answered 15/10, 2013 at 23:10 Comment(5)
What version of Entity Framework?Ram
I believe the EF Power Tools have reverse engineering a database as a feature: visualstudiogallery.msdn.microsoft.com/…Anomaly
@millimoose: EF Power Tools do not address database projects.Goldsberry
@ErikPhilips: I'm usign EF5 but I can wait to EF6...Bachelor
this feature would be really useful imo, but as far as I have searched it is not ther even for vs 2013Sporophyte
B
1

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.

Bangor answered 26/5, 2015 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.