I have 3 Tables. Metadata, Rules, and a NxN relationship MetadataRules.
I'm inserting a Metadata, and my object contains a list of Rules that are retrieved from DB.
myMetadata.Rules = myListOfrules;
When i perform an insert, all the rules in myListOfRules are duplicated in the Rules table, instead of just creating a relationship. I'm inserting it with:
public static void InserirTipoMetadata( TA_TIPO_METADATA tipoMetadata ) {
using ( EnterpriseContext context = new EnterpriseContext() ) {
context.TipoMetadata.AddObject(tipoMetadata);
context.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
}
}
What should i do to not duplicate the Rules?
Thanks!
I found a solution, but I don't think it is the right one...
public static void InserirTipoMetadata( TA_TIPO_METADATA tipoMetadata ) {
using ( EnterpriseContext context = new EnterpriseContext() ) {
List<TA_REGRA_VALID> regras = new List<TA_REGRA_VALID>();
foreach ( var v in tipoMetadata.TA_REGRA_VALID ) {
regras.Add(context.Regra.Single(p => p.CO_SEQ_REGRA == v.CO_SEQ_REGRA));
}
tipoMetadata.TA_REGRA_VALID = regras;
context.TipoMetadata.AddObject(tipoMetadata);
context.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
}
}
I'm selecting the rules again from the DB before trying to add. Is that the correct way?
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.