EF6 include/select multiple entity levels throws NotSupportedException - mysql

This seems very much similar to: EF4.1 multiple nested entity Includes gets NotSupportedException?
But the exception message is different and we are using a newer libraries.
Trying to load an entity graph with 3 levels made of: Accounting Record which contains multiple AccountingOperation(s) which in turn each of them contain multiple AccountingOperationLine(s). The exception below occurs if I want to load the 3 nevigation properties on the 3rd level (1 synthetical and 2 analytical accounts) as shown below.
Code:
var v = dbEntities.Set<AccountingRecord>()
.Include(ar => ar.AccountingRecordOperations.Select(
aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingSynthetic)))
.Include(ar => ar.AccountingRecordOperations.Select(
aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingAnalytical1)))
.Include(ar => ar.AccountingRecordOperations.Select(
aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingAnalytical2)))
.ToList();
Exception
NotSupportedException: All objects in the EntitySet 'DbEntities.Entities' must have unique primary keys.
However, an instance of type '...AccountingSynthetic' and an instance of type '...AccountingRecordOperation' both have the same primary key value, 'EntitySet=Entities;ID=1104'
Something clearly confuses EF since AccountingSynthetic and AccountingRecordOperation don't share the id.
Configuration:
Model first
TPT
MySQL 5.6
MySQl Connector 6.8.3
EF 6.0
Notes:
All entities inherit of Entity.
The synthetic and analytical accounts all of them have a one to many relation to AccountingOperationLines (could this confuse EF?)
The error persist even with this simpler query like:
var v = dbEntities.Set<AccountingRecord>()
.Include(ar => ar.AccountingRecordOperations.Select(aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingSynthetic)));

Related

update database : error asp.net core with mysql

im using mysql database with EF core .
i use Identity Schema to my existed MySql database , after doing a migration , update database generate this :
"A schema "Identity" has been set for an object of type "CreateTableOperation" with the name of "Role". MySQL does not support the EF Core concept of schemas. Any schema property of any "MigrationOperation" must be null. This behavior can be changed by setting the SchemaBehavior option in the UseMySql call."
error pic
So how to set the schemabehavior option .
With gratitude.
Update your service registrator (Startup.cs) and modify the DB context to set the SchemaBehavior option as one of the following:
// Throw an exception, if a schema is being used. This is the default.
options.UseMySql(myConnectionString, b => b.SchemaBehavior(MySqlSchemaBehavior.Throw))
// Silently ignore any schema definitions.
options.UseMySql(myConnectionString, b => b.SchemaBehavior(MySqlSchemaBehavior.Ignore))
// Use the specified translator delegate to translate from an input schema and object name to
// an output object name whenever a schema is being used.
options.UseMySql(myConnectionString, b => b.SchemaBehavior(MySqlSchemaBehavior.Translate,
(schema, entity) => $"{schema ?? "dbo"}_{entity}"))
// Just ignore all schemas:
optionsBuilder
.UseMySql(
connectionString,
serverVersion,
o => o.SchemaBehavior(MySqlSchemaBehavior.Ignore))
// Translate schemas into a table name prefix like `schema_table`:
optionsBuilder
.UseMySql(
connectionString,
serverVersion,
o => o.SchemaBehavior(MySqlSchemaBehavior.Translate, (schema, table) => $"
{schema}_{table}"))

Laravel Eloquent create method skipping IDs and returning them

I'm simply trying to create a new model through Eloquent's create method. However, we're seeing that the new model will sometimes contain an id that has been skipped in mysql. This id is designated as an autoincrement column in mysql.
We are also seeing this happen when using DB:getPdo()->lastInsertId() immediately after the model has been created.
$questionResponse = $response->questionResponses()->create([
'survey_question_id' => $question->id,
'response' => 5
]);
$response_id = $questionResponse->id; // weve also used DB::getPdo()->lastInsertId();
Queue::push('TheWorker', array('survey_id' => $survey->id, 'response_id' => $response_id, 'question_id' => $question->id, 'user_id' => $user->id, 'student_target_id' => $student_target_id));
TheWorker then fails due to receiving an incorrect $repsonse_id that doesn't exist in the DB.
We would expect that the newly created model would contain the id that matches the record created in mysql.
Thanks in advance!

What does _fpt_ stand for in semantic mediawiki

In database I have two types of tables [based on their name]
Example:
1. smw_di_... => [semantic mediawiki data item => as I understood]
2. smw_fpt_... => [??]
And what is the key s_id
Does someone know?
It's important to me to understand the logic, because no books are available, no documentation...
FPT - Fixed Property Table
Fixed Properties are properties that are user defined but intensively used in the wiki
extensions/SemanticMediaWiki/src/SQLStore/PropertyTableInfoFetcher.php
private $fixedSpecialProperties = array(
// property declarations
'_TYPE', '_UNIT', '_CONV', '_PVAL', '_LIST', '_SERV',
// query statistics (very frequently used)
'_ASK', '_ASKDE', '_ASKSI', '_ASKFO', '_ASKST', '_ASKDU',
// subproperties, classes, and instances
'_SUBP', '_SUBC', '_INST',
// redirects
'_REDI',
// has sub object
'_SOBJ',
// vocabulary import and URI assignments
'_IMPO', '_URI',
// Concepts
'_CONC'
);
s_id => pointer [foreign key to smw_object_ids smw_id]
p_id => property id if it's not fixed DI

EF4.1 Eager loaded linked objects are returned null

Can anyone explain why a Company is returned but Company.CompanyServices is null (even though I've created one in the test)?
public List<Company> GetContactCompanies(int contactId)
{
var query = (
from directorCompany in ctx.CompanyDirectors
.Where(d => d.ContactAddress.Contact.Id == contactId)
.Include(d => d.Company.CompanyServices)
select directorCompany.Company
).OrderBy(c => c.CompanyName).Distinct();
return query.ToList();
}
Note substituting the Include for .Include("Company.CompanyServices") has no effect
Is the Company.CompanyServices property marked as virtual? Check out ScottGu's blog on entity framework, where he creates POCO classes with one to many relationships he marks the collection properties as virtual.
When I first started using EF 4, that had me stumped for quite a while.
Obviously I can't see your entity classes so this may be a moot point!
Found an answer that's not wholly inuitive, but it works which is the main thing.
Happy to see one that plays on the original query...
var query = (
from directorCompany in ctx.CompanyDirectors
.Where(d => d.ContactAddress.Contact.Id == contactId)
select directorCompany.Company
).OrderBy(c => c.CompanyName).Distinct();
return query.Include(c => c.CompanyServices).ToList();

Why LINQ 2 SQL when I Dispose the context tries to get child relationships?

I use LINQ 2 SQL in one of my projects and i have numerous relationships Customer -> Documents1, Documents2, Documents3, Address, Invoices etc....
When using the LoadWith(p => p.Documents1)...etc
I have performance issues, imagine 2000 customers with all these numerous relationships loaded in List in memory!
The other way around Document -> Customer its not so much an issue as the relationship its light.
So i try to remove all the LoadWith and just leave the Customer -> Address relationship.
Now if i go and open a Document1 and then open my Customers i get an object disposed exception when i serialize my Customers. The serialize method basically throws this exception.
The serialize Method:
public static T CloneObjectGraph<T>(this T obj) where T : class
{
var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
using (var ms = new System.IO.MemoryStream())
{
serializer.WriteObject(ms, obj);
ms.Position = 0;
return (T)serializer.ReadObject(ms);
}
}
The Method i get the Customers:
public List<Customer> GetCustomers()
{
using (MyDataContext db = new MyDataContext(MyDataContextManager.ConnectionString))
{
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Customer>(p => p.Address);
dlo.LoadWith<Customer>(p => p.Documents1);
dlo.LoadWith<Customer>(p => p.Documents2);
dlo.LoadWith<Customer>(p => p.Documents3);
dlo.LoadWith<Customer>(p => p.Documents4);
dlo.LoadWith<Customer>(p => p.Documents5);
dlo.LoadWith<Customer>(p => p.Documents6);
dlo.LoadWith<Customer>(p => p.Documents7);
dlo.LoadWith<Customer>(p => p.Documents8);
dlo.LoadWith<Customer>(p => p.Documents9);
dlo.LoadWith<Customer>(p => p.Documents10);
dlo.LoadWith<Customer>(p => p.Documents11);
db.LoadOptions = dlo;
return db.Customers.ToList();
}
}
I want to remove all the LoadWith except Address relationship.
I hate when this error is not reproduce always but in some cases i couldn't find.
I could guess the DataContractSerializer constructor for a change but i cant get it right.
Any help appreciated!
Your error is occurring because your clone method is going to attempt to access all of the child properties of your object. When you have the LoadWith<>() statements, those child properties are already retrieved from the database and available in memory. When you remove the LoadWith<>() statements, the properties will attempt to Lazy Load the objects from the database. Because you've already closed the database connection, those properties cannot be loaded (and you get the error).
The solution depends on what you are attempting to accomplish by performing a deep copy of the objects; If that deep copy actually does need to have the child properties (DocumentsXX), then you either have to leave the LoadWith<>() statements or you need the database connection to stay open during the process (of the two, leaving the LoadWith<>() statements is probably the better option because it minimizes the accesses to the DB and ends up using the same amount of memory anyway). If the deep copy really doesn't need to include those properties, then you need to replace the current deep clone process with on that can ignore those properties (either a manually created deep clone or a customized serialization process would work).