0

I've searched but I guess I'm not exactly sure what to search on for this...

I am writing a data access layer for my application using the entity framework. I am performing a database first method so the entity data model is auto-generated.

So the model.context.cs file under the model.edmx contains this:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace com.MyCompany.AppPro.DAL 
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class AppProEntities : DbContext
    {
        public AppProEntities()
            : base("name=AppProEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<UserData> UserData { get; set; }
        public virtual DbSet<UserKey> UserKey { get; set; }
    }
}

I need to update this code to accept a connection string outside of this class. But when I update the model this file gets overwritten (which I'm warned about) in the comments. I can keep putting the code back in each time I update but there has to be a better way...

Is there any way I can add additional code into this file and have it retained on a DB schema update?

1 Answer 1

1

You need to define the same partial class inside the same namespace with your custom code in a separate file, this is the whole point in using a partial class.

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

2 Comments

thanks, sometimes you can't see the trees through the forest.
@DaBlue please consider flagging my reply as an answer instead of an upvote, thanks.

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.