-3

I've got a problem with a dll supplied to me by a someone. they are using entity framework with sql, they have provided me with a dll (for the classes) and I cannot for the life of me get the data into the models in the dll.

If I recreate the sql from scripts I can do it (bu creating my own entity from code), but I need to use the dll to populate data from the db etc.

I have attached the dll and rwl (database connections) https://wetransfer.com/downloads/63970cd823d964a68c50fef904e3697e20200107065132/351fac9f55f3ae92fe985c9a01a69deb20200107065132/1ae840?utm_campaign=WT_email_tracking&utm_content=general&utm_medium=download_button&utm_source=notify_recipient_email

anyone that can help?

this is my code for a local db using the self created entityframework models. but i need to use the dll's models

string connStr = @"Data Source=DESKTOP-5T9EDLN;Initial Catalog=CRM;User Id=sa;Password=PW;Application Name=JobSupervisor;Connection Timeout=120;";

var db = new RWL.DataBase.cDB();

string stre = "select CJ.JobNumber, CJ.SupervisorID, CJ.CustomerName from   crm.tbljobs CJ      left join [Order] O on O.JobID = CJ.JobID       left join materials.OrderCode OC on OC.ID = O.OrderCodeID " +
    "left join TaskOrder TOR on TOR.OrderCodeID = OC.ID " +
    "left join Projects.Task T on T.ID = TOR.TaskID " +
    "left join Projects.ProjectTask PT on PT.TaskID = T.ID " +
    "left join Projects.Project P on P.ID =  PT.ProjectID " +
    "left join Projects.JobProject JP on JP.ProjectID = P.ID " +
    "where " +
    "PT.ActualStart is NULL " +
    "--and P.StartDate IS NOT NULL  " +
    "--and P.FinishDate IS NULL " +
    "order by CJ.JobNumber ";

db.ConnectionString = connStr;

DataTable project = new DataTable();

project = db.GetDataTable(stre);

gridControl1.DataSource = project;

I need to use Linq to SQL to create the query and use dll to populate data, not SQL like in my code above.

Here is the code that works when i create the new entity model "model1" in my winforms project, but like i said, i need to use their dll ("Model.dll"), not my own one.

using (var db = new Model1())
        {

            var query = from b in db.tblSupervisors
                        orderby b.Surname
                        select b;

            foreach (var item in query)
            {
                var obj = 
                cboSupervisors.Properties.Items.Add(item.FirstName + " " + item.Surname);
            }
        }

EDIT-Update When trying to use Enable-Migration in nuget package console I now get "Job_Supervisor.DAL.tblJob: : EntityType 'tblJob' has no key defined. Define the key for this EntityType."

It seems like the dll is now being used but it's missing keys, which wouldn't make sense for them to provide this dll then, am I missing something?

1 Answers1

0

In the DLL you received check if the application DB context is available. Hopefully it should be. You can access all the entities through that. Make sure you have specified the connection string properly. And also that you have migrated the entities to your own database which you are working right now. (Link: Entity Framework : How to migrate to new database)

Since you are trying to join several entities, have a look at this link which will greately benefit you.

Additionally if for any reason you could not update the tables you need to then try using the ExecuteSqlCommand available in entity framework.

--UPDATED--

It seems your DLL only has the model classes. Thats it. You have to create the entity framework code from ground up.

1) So in entity framework first you need to add the EF dll reference to the project . In Visual Studio Select Tools > Nuget package manager > Manage Nuget package for solution. Search for EntityFramework and install the nuget. A reference dll will be included in your solution.

2) Create the ApplicationDbContext class. Create a new class and inherit DbContext class

using System.Data.Entity;

namespace YourNamespace
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext()
            : base("Name=ApplicationDbContextConnection")
        {
        }

        public virtual DbSet<crm.tbljobs> Jobs { get; set; }
        public virtual DbSet<TaskOrder> TaskOrders { get; set; }
        public virtual DbSet<Projects> Projectz{ get; set; }

Here, ApplicationDbContextConnection is the connection string name.

3) Using the ApplicationDbContext class in your solution

In the code file where you need to use the entity models first declare the class on top

private readonly ApplicationDbContext _context;

Then use it in your code. Example:

_context = new ApplicationDbContext();
//now you can access the models in the Model.DLL since you have already included it as a DBSet in the earlier step
//_context.Jobs
//_context.TaskOrders
//_context.Projectz
//This way you are using the models in the DLL

Make sure you migrate it to your own database before you run the project. Link is given above. And to join the different tables using entity framework use the sample my second link. Good luck !

sm101
  • 562
  • 3
  • 10
  • 28
  • Thanks for your response. I cannot see the DBContext. the dll is called model.dll and i have added it in references, it exposes the classes for the tables, but i cannot add them to lists etc. it gives me errors - " is a type" i see there is no place for me to upload the dll here – Naas Prinsloo Jan 07 '20 at 06:41
  • @NaasPrinsloo ok it seems you only have the model class declarations in the DLL. So now you have to do some more work in your solution to access the db tables using Entity framework. Let me add some more code/edit in my answer so you can do crud using entity framework – sm101 Jan 07 '20 at 06:52
  • I'm busy uploading the dll's for you. hopefully you'll understand what i mean and can maybe then help. thanks again – Naas Prinsloo Jan 07 '20 at 06:53
  • link to files https://wetransfer.com/downloads/63970cd823d964a68c50fef904e3697e20200107065132/351fac9f55f3ae92fe985c9a01a69deb20200107065132/1ae840?utm_campaign=WT_email_tracking&utm_content=general&utm_medium=download_button&utm_source=notify_recipient_email – Naas Prinsloo Jan 07 '20 at 06:54
  • @NaasPrinsloo updated the answer – sm101 Jan 07 '20 at 07:19
  • I've almost got it working. I'm currently getting "Job_Supervisor.DAL.tblJob: : EntityType 'tblJob' has no key defined. Define the key for this EntityType." on the data query. current query i'm using is this `string connStr = @"Data Source=DESKTOP-5T9EDLN;Initial Catalog=CRM;User Id=sa;Password=MootcoM5;Application Name=JobSupervisor;Connection Timeout=120;"; _context = new ApplicationDbContext(); List job = _context.Jobs.ToList();` – Naas Prinsloo Jan 07 '20 at 07:40
  • Unfortunately I cannot seperate this question from the current one as stack-overflow is prohibiting me from creating another question that relates to the error message – Naas Prinsloo Jan 07 '20 at 08:26
  • Thanks for your help @sm101 it's not sorted yet due to the keys not being defined in the dll i was given, but your advise was 100% what i was looking for. thanks – Naas Prinsloo Jan 07 '20 at 12:18
  • @NaasPrinsloo Your welcome ! As a workaround you could perhaps use a DLL decompiler like DotPeek (https://stackoverflow.com/questions/18050615/how-to-open-dll-files-to-see-what-is-written-inside/18050646) and extract the Model code from there. Then create your own Model classes and add the [Key] attribute for the ones you need to ad keys (sample key with auto-increment: http://dotnetreads.blogspot.com/2019/07/entity-framework-adding-auto-increment.html) – sm101 Jan 08 '20 at 11:28