1

I'm working with an older database that we'd like to make some changes to. I've been reading about the fluent API which I like, however the more I read through it the more it seems like that has to be approached from a code first perspective. What I'd like to know is, is there any way to utilize the fluent API when you have the database first? Some things I'm looking to do would be for example refactor some column names on the database side and have them mapped to the existing data model, so that I can change them gradually throughout the application. I'm wondering if anyone has had any experience doing this and what would be the best place to start.

3
  • You should be able to generate C# POCO classes from an existing database and then use those classes. Is your intent to leave the database intact, and change the object model to make it more user friendly? Commented Nov 7, 2016 at 21:02
  • @Michaelbuller No, my main goal is to gradually transition the database and bring it up to standards. So for example I'd leave the POCO objects in the application as for ex: dtUpdated, but in the database I'd change it to EntryTs and just have it mapped to an POCO as dtUpdated. That way I can go through the application as time permits and change the name later (the property name may be used as part of the JavaScript or with AutoMapper so it's difficult to just use the built in refactoring tools without looking deeper into it). Commented Nov 7, 2016 at 21:28
  • You can use the Table or Column annotations if you want to give your objects one name and have them point to a database object. Example, [Table("User", Schema = "security")] public partial class AppUser and with a column, [Required] [Key] [Column("Id")] public Guid UserId { get; set; } Commented Aug 10, 2018 at 14:54

1 Answer 1

2

Entity Framework is an ORM between your application and your database. You can start model first, code first or database first and still use the features. If you are worried about what Entity Framework will do to your database, then you want to look into migrations. If migrations are configured to auto and to allow data loss, then you could lose data by making changes in your classes. If you manage it correctly though, you could use EF to model the database as is, then use the migrations to actually bring the database along. I will say that the Fluent API is really tailored to code first and doing the below will actually be more work than generating POCO from your database and just using the code. Either way, I hope this helps.

You are going to have to create a configuration that derives from EntityTypeConfiguration

So with your example,

public class CustomerConfiguration: EntityTypeConfiguration<Customer>;
{

    public CustomerConfiguration(): base()
    {

    HasKey(p => p.Id);
    Property(p => p.Id).
        HasColumnName("Id").
        HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).
        IsRequired();
    Property(p => p.dtUpdated).
        HasColumnName("EntryTs").
        IsRequired();
    ToTable("Customers");
    }

}

Or with fluent api... modelBuilder.Entity<ClassName>().ToTable("DatabaseTableName", "DatabaseSchema");

https://msdn.microsoft.com/en-us/library/jj591617(v=vs.113).aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.