0

I want to embed my entity datamodel to my class library that contains methods of database operations. (like MyEntityDatamodel.dll) Simply when i add my entity framework embedded class library to my windows forms application, i am able to use it's methods (insert update delete).

Currently i am able to running methods from dll but the problem is i must add EntityFramework.dll and EntityFramework.SqlServer.dll to my forms application as reference. Otherwise it doesn't work. I want to add only MyEntityDatamodel.dll.

Sample code of my entity datamodel class:

public class MyClass
{

   public string classParameter { get; set; }

   public void InsertMethod(MyClass parameter)
   {
       var dbContext = new MyEntities();
       InsertOperations...
   }

   public void UpdateMethod(MyClass parameter)
   {
       var dbContext = new MyEntities();
       UpdateOperations...
   }
}

How I am using;

using MyClass;

MyClass myClass = new MyClass();
myClass.classParameter = "example";

myClass.InsertMethod(myClass);

Thanks in advance.

2
  • What problem are you trying to solve by doing what you explain? Commented Sep 3, 2015 at 14:37
  • Simply way, i want to reference only one dll that contains database operations with entity data model to my winforms project. This dll have to include entity data model and it's auto generated classes, entitity framework's library dll's. After that i want to use insert-update-delete methods from my winform project via this dll. I can use it now as i described. But the problem is i must add Entity Framework's base dll's as reference to my winform project. I don't want this. I want to work single dll. Commented Sep 3, 2015 at 14:53

2 Answers 2

1

Two things to note...

First, directly to your question - you can't. You may try some sophisticated techniques like dynamically loading the assemblies your program needs, but this changes almost nothing except that you do not have them listed in your project references. They still need to be somewhere where the loader can find them. And more important they must always be deployed together with your app. Otherwise it won't work.

Second, DbContext is meant to be used as a Unit-of-Work design pattern, that is it needs to be disposed after the unit has finished whatever it was meant to do. Usualy, the using clause is a very good choice for this, so consider wrapping your code this way:

using (var dbContext = new MyEntities())
{
   InsertOperations.../UpdateOperations...
}
Sign up to request clarification or add additional context in comments.

1 Comment

In my personal case, I defined an DbContext extension inside the library with all the model configurators. Later from my main project I create instances of that extended DbContext and I can operate correctly. I tried to have a factory in the library but it isn't working well.
1

After spending hours of on this, finally i found a solution. I am able to inject connection string to my dbcontext before my dbcontext object was created.

First, i changed my dbcontext's constructor method. I added string parameter for connection string instead reading from the web.config.

I changed this;

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

To this;

    public partial class ExampleEntities : DbContext
    {
        public ExampleEntities(string connStr) : base(connStr)
        {
        }
    }

Then i added a method for Generating connection string to my code;

        public static string CreateConnStr(string dataSource, string instanceName, string userName, string password)
        {
            string connectionString = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder
            {
                Metadata = "res://*/ExampleModel.csdl|res://*/ExampleModel.ssdl|res://*/ExampleModel.msl",
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
                {
                    InitialCatalog = instanceName,
                    DataSource = dataSource,
                    IntegratedSecurity = false,
                    UserID = userName,
                    Password = password,              
                }.ConnectionString
            }.ConnectionString;

            return connectionString;
        }

Now i am simply use my dbcontext shown as below;

var dbContext = new ExampleEntities(CreateConnStr("localhost\\SQLEXPRESS","ExampleDataBase","UserName", "Password"));

Hope it helps.

P.s.: Sorry for bad grammar. :)

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.