简体   繁体   中英

Entity Framework dbset attach non-static method requires a target

I'm manually loading a DBSET from the EF, and the base mapping works. The error occurs when that same DBSET must be used for another query with lazy loading, it returns the error "non-static method requires a target", Example:

// class 1
public Transaction GetTx(int id){
    // load data to POCO with other source

    Transaction tx = new Transaction() { prop1 = 1, prop2 = "xx");
    ctx.Transactions.Attach(tx);
    return tx;
}

// class 2
public void OtherMethod(){
    Transaction tx = GetTx(1);
    // do something with tx

    // load with lazy loading
    var detail = context.Detail.Where(x => x.Service.Code == tx.Port.Service.Code).ToList(); // here occurs error "non-static method requires a target"
}

How can I manually load the DBSET and then continue the lazy loading of the other related tables?

EDIT 1:

I'm using load offline, because I'm having performance issues. I am migrating an old project with "Database First" to "Code First", the EF code is the same as always, but the mapping has changed (I am working with Oracle 10g). For some reason with DB First the query executes in less than 50ms, but with Code First it takes more than 3 seconds and is a critical service.

If I offline load the main table, it stays within 50ms. What could I change to have the same DB First performance with Code First, without having to change the logic?

PS: I use.Net 4.5.1, EF 6.2, Oracle 10g and I don't have many chances to update these features for a few months (company policies that I can't modify)

You don't use DbSet offline. It requires a database connection. In offline scenarios you can use the Sqlite provider or (for testing) the in-memory provider .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM