I am not able to add new child entity while updating - entity-framework

I am not able to add child entity while updating through EF.
public virtual void TestUpdate(object item)
{
var props = item.GetType().GetRuntimeProperties().Where(x => (x.PropertyType.FullName.StartsWith("MIRRA"))||(x.PropertyType.FullName.StartsWith("System.Collections.Generic.ICollection"))).Select(m => m).ToList();
foreach (var prop in props)
{
object value = prop.GetValue(item);
if (value is IEnumerable)
{
foreach (var listitem in value as IEnumerable)
{
TestUpdate(listitem);
}
}else if (value != null)
{
root = value;
TestUpdate(value);
}
}
int id = (int)item.GetType().GetProperties().FirstOrDefault().GetValue(item);
if (id == 0)
{
context.Entry(item).State = EntityState.Added;
//add new child entity to context
}
else
{
context.Entry(item).State = EntityState.Modified;
}
}
at last I am saving this context but context not adding child entities

Related

swashbuckle openapi 3 write example and description for the dynamically generated model classes

My model properties definition is coming from a json file so using reflection to write the classes to be shown under schema on resulting swagger page.
foreach (var model in Models)
{
if (!ModelTypes.ContainsKey(model.Key))
{
anyNonCompiledModel = true;
BuildModelCodeClass(modelComponentBuilder, model.Value);//Build model classes
}
}
BuildModelCodeEnd(modelComponentBuilder);
if (anyNonCompiledModel)
{
CSharpCompiler compiler = new CSharpCompiler();
compiler.AddReference(typeof(object));
compiler.AddReference(typeof(ResourceFactory));
compiler.AddReference(typeof(System.Runtime.Serialization.DataContractResolver));
compiler.AddReference(typeof(System.Runtime.Serialization.DataContractAttribute));
var types = compiler.Compiler(modelComponentBuilder.ToString()); //write model classes
foreach (var type in types)
{
ModelTypes.Add(type.Name, type);
}
}
public void BuildModelCodeClass(StringBuilder modelComponentBuilder, MetadataModelEntity model)
{
modelComponentBuilder.AppendLine($"public class {model.Name} {{");
foreach (var p in model.Data.Properties)
{
if (p.Obsoleted) continue;
if (p.Type.Type == "array")
{
modelComponentBuilder.AppendLine($" public {p.Type.ArrayType.ObjectName}[] {p.Name} {{get;set;}}");
}
else
{
//primitive types
modelComponentBuilder.AppendLine($" public {p.Type.ObjectName} {p.Name} {{get;set;}}");
}
}
modelComponentBuilder.AppendLine(
#"}
");
}
If i provide the description and example like following (in BuildModelCodeClass, inside the loop) then the example and description displays for me.
if (!string.IsNullOrWhiteSpace((string)p.Example))
{
modelComponentBuilder.AppendLine($" ///<example>{p.Example}</example>");
}
if (!string.IsNullOrWhiteSpace((string)p.Description))
{
modelComponentBuilder.AppendLine($" ///<description>{p.Description}</description>");
}
However, i dont want to do above.
I want to write my models via the open api and not via the C# Compiler, is it possible?
I want to show example and description via schema (may be under paths some where). How can i do this? Context has my models info available that i can interact with here.
public class SwaggerDocumentFilter : IDocumentFilter
{
SwaggerDocument _swaggerDocument;
public SwaggerDocumentFilter(object apiConfigure)
{
_swaggerDocument = ((ApiGatewayConfiguration)apiConfigure).SwaggerDocument;
}
public void Apply(OpenApiDocument document, DocumentFilterContext context)
{
if (document.Info.Extensions == null || !document.Info.Extensions.ContainsKey(SwaggerEndpoint.ExtensionDocName)) return;
var openIdString = document.Info.Extensions[SwaggerEndpoint.ExtensionDocName] as OpenApiString;
if (openIdString == null) return;
var docName = openIdString.Value;
SwaggerEndpoint endpoint = _swaggerDocument.SwaggerEndpoints.SingleOrDefault(x => x.Name == docName);
if (endpoint == null) return;
//Add server objects
document.Servers = endpoint.ServerObjects;
//Add Tags objects
document.Tags = endpoint.Tags;
//Set swagger paths objects
var pathsObjects = _swaggerDocument.GetPathsObject(docName, context);
if (pathsObjects.IsValid())
{
pathsObjects.ToList().ForEach(
item => document.Paths.Add(item.Key, item.Value)
);
}
//Add Schema components
//Add Example/Examples
}
}
Following helped
https://github.com/domaindrivendev/Swashbuckle.WebApi/issues/162
AddSchemaExamples.cs
public class AddSchemaExamples : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
if (type == typeof(Product))
{
schema.example = new Product
{
Id = 123,
Type = ProductType.Book,
Description = "Treasure Island",
UnitPrice = 10.0M
};
}
}
}
SwaggerConfig.cs
httpConfig
.EnableSwagger(c =>
{
c.SchemaFilter<AddSchemaExamples>()
});
My implementation for the Apply since model is dynamic
if (model != null)
{
schema.Description = model.Description;
foreach (var p in schema.Properties)
{
var mp = model.Data.Properties.SingleOrDefault(x => x.Name == p.Key);
if (mp != null)
{
if (!string.IsNullOrWhiteSpace(mp.Description))
{
p.Value.Description = mp.Description;
}
if(!string.IsNullOrWhiteSpace(mp.Example))
{
p.Value.Example =
new Microsoft.OpenApi.Any.OpenApiString(mp.Example.ToString());
}
}
}
}

Entity Framework Not Saving Child Link

I am inserting or updating a parent entity with associated children entities. Entity framework inserts or updates the entities BUT it never creates or updates the links between them even though they exist in C#.
public void MergeParent(Parent parent)
{
// Set Parent
var parentInDB = context.Parents.SingleOrDefault(p => p.ParentID == parent.ParentID);
if (parentInDB != null)
{
parent.ID = parentInDB.ID;
context.Entry(parentInDB).CurrentValues.SetValues(parent);
}
else
{
context.parents.Add(parent);
}
// Set child
foreach(var child in parent.children)
{
var childInDB = context.children.SingleOrDefault(a => a.DiscogsID == child.DiscogsID && child.DiscogsID != null);
if (childInDB != null)
{
child.ID = childInDB.ID;
context.Entry(childInDB).CurrentValues.SetValues(child);
}
else
{
context.children.Add(child);
}
}
// Finally Save
context.SaveChanges();
}
If I set a breakpoint, I can see the parent has the children associated, but while the database contains the elements it does not ever update the link table.
// Set child
foreach(var child in parent.children)
{
var childInDB = context.children.SingleOrDefault(a => a.DiscogsID == child.DiscogsID && child.DiscogsID != null);
if (childInDB != null)
{
child.ID = childInDB.ID;
context.Entry(childInDB).CurrentValues.SetValues(child);
}
else
{
context.children.Add(child);
}
}
The problem is in the section above, for the children, you're adding to and retrieving from the database instead of from the parentInDb's collection of children. I don't think SetValues updates relationships.
I modified some of your code and added some comments:
// Add an include to bring in the children
var parentInDB = context.Parents.Include(p => p.children)
.SingleOrDefault(p => p.ParentID == parent.ParentID);
if (parentInDB != null)
{
// This line shouldn't be necessary...
// parent.ID = parentInDB.ID;
context.Entry(parentInDB).CurrentValues.SetValues(parent);
// Set child
foreach(var child in parent.children)
{
// Should search in parentInDB, not context
// If DiscogsID is a primary key, it can't be null
var childInDB = parentInDB.children.SingleOrDefault(a => a.DiscogsID == child.DiscogsID);
if (childInDB != null)
{
// This line shouldn't be necessary...
// child.ID = childInDB.ID;
context.Entry(childInDB).CurrentValues.SetValues(child);
}
else
{
// Should add to parentInDB, not context
parentInDB.children.Add(child);
}
}
// Would children in parentInDB.children not found in parent.children need to be removed?
}
else
{
// This will automatically add untracked children
context.parents.Add(parent);
}
// Finally Save
context.SaveChanges();
I don't know what happens if you add a new parent with previously attached entities in children.

Add OR condition to query

I am wondering how it is possible to add an OR condition to the Envers criteria api:
public IEnumerable<Guid> GetHistory(object id, params string[] props)
{
var auditQuery = AuditReaderFactory.Get(Session).CreateQuery()
.ForRevisionsOfEntity(typeof(T), false, true);
foreach (var prop in props)
{
auditQuery.Add(AuditEntity.RelatedId(prop).Eq(id)); // <-- adds AND, while OR is required!
}
return auditQuery
.GetResultList<object[]>()
.Select(i => ((T)i[0]).ID)
.Distinct();
}
Use AuditEntity.Disjunction().
In your example, something like...
[..]
var disjunction = AuditEntity.Disjunction();
foreach (var prop in props)
{
disjunction.Add(AuditEntity.RelatedId(prop).Eq(id));
}
auditQuery.Add(disjunction);
[..]
I did like this in Java as #Roger mentioned above. (Just in case if anybody needs)
public List<Employee> getAuditHistory(Session session, int id, String property) {
AuditReader auditReader = AuditReaderFactory.get(session);
List<Employee> employeeHistory = new ArrayList<>();
if (auditReader != null) {
AuditQuery auditQuery = auditReader.createQuery().forRevisionsOfEntity(Employee.class, true, false)
.add(AuditEntity.property(ResultsConstants.Employee_ID).eq(id));
AuditDisjunction auditDisjunction = null;
if (property.equalsIgnoreCase("FULL_NAME")) {
auditDisjunction = AuditEntity.disjunction().add(AuditEntity.property("FIRST_NAME".toUpperCase()).hasChanged())
.add(AuditEntity.property("LAST_NAME".toUpperCase()).hasChanged());
} else {
auditQuery = auditQuery.add(AuditEntity.property(property.toUpperCase()).hasChanged());
}
auditQuery = auditQuery.addOrder(AuditEntity.property("MODIFIED_DATE").desc());
if(null != auditDisjunction){
auditQuery = auditQuery.add(auditDisjunction);
}
if (auditQuery != null) {
if (auditQuery.getResultList().isEmpty()) {
// Log here or throw it back to caller
}
employeeHistory.addAll(auditQuery.getResultList());
}
}
return employeeHistory;
}

how to add extend breeze entity types with metadata pulled from property attributes

I want to get the custom attributes, mentioned below, in breeze dataService (client side).
namespace Tam.Framework.Web.Models
{
[ViewAttribute("app/views/Employee.html")]//this custom class attribute
public class Employee : BaseEntity
{
protected override string OnGetDescriptor()
{
return "some description";
}
public string FirstName { get; set; }
[Display(Name = "LAST NAME")]//this custom property attribute
public string LastName { get; set; }
}
}
On the server, add logic to the Metadata controller action to supplement the standard metadata with the display attribute properties:
[HttpGet]
public virtual string Metadata()
{
// Extend metadata with extra attributes
var metadata = JObject.Parse(this.ContextProvider.Metadata());
var ns = metadata["schema"]["namespace"].ToString();
foreach (var breezeEntityType in metadata["schema"]["entityType"])
{
var typeName = ns + "." + breezeEntityType["name"].ToString();
var entityType = BuildManager.GetType(typeName, true);
foreach (var propertyInfo in entityType.GetProperties())
{
var attributes = propertyInfo.GetAllAttributes();
var breezePropertyInfo = breezeEntityType["property"].SingleOrDefault(p => p["name"].ToString() == propertyInfo.Name);
if (breezePropertyInfo == null)
continue;
// handle display attribute...
var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
if (displayAttribute != null)
{
var displayName = displayAttribute.GetName();
if (displayName != null)
breezePropertyInfo["displayName"] = displayName;
var displayOrder = displayAttribute.GetOrder();
if (displayOrder != null)
breezePropertyInfo["displayOrder"] = displayOrder;
var autogenerateField = displayAttribute.GetAutoGenerateField();
if (autogenerateField != null)
breezePropertyInfo["autoGenerateField"] = autogenerateField;
}
// allowEmptyStrings.
if (propertyInfo.PropertyType == typeof(string))
{
breezePropertyInfo["allowEmptyStrings"] = true;
var requiredAttribute = attributes.OfType<RequiredAttribute>().FirstOrDefault();
if (requiredAttribute != null && !requiredAttribute.AllowEmptyStrings)
breezePropertyInfo["allowEmptyStrings"] = false;
}
// todo: handle other types of attributes...
}
}
return metadata.ToString();
}
On the client, fetch the metadata and supplement the breeze entity type with the custom metadata.
function initializeMetadataStore(metadataStore, metadata) {
var metadataType, metadataProperty, entityProperty, i, j;
for (i = 0; i < metadata.schema.entityType.length; i++) {
metadataType = metadata.schema.entityType[i];
var entityType = metadataStore.getEntityType(metadataType.name);
for (j = 0; j < metadataType.property.length; j++) {
metadataProperty = metadataType.property[j];
entityProperty = entityType.getProperty(metadataProperty.name);
if (entityProperty) {
if (typeof metadataProperty.displayName !== 'undefined') {
entityProperty.displayName = metadataProperty.displayName;
}
if (typeof metadataProperty.displayOrder !== 'undefined') {
entityProperty.displayOrder = metadataProperty.displayOrder;
}
if (typeof metadataProperty.autoGenerateField !== 'undefined') {
entityProperty.autoGenerateField = metadataProperty.autoGenerateField;
}
if (typeof metadataProperty.allowEmptyStrings !== 'undefined') {
entityProperty.allowEmptyStrings = metadataProperty.allowEmptyStrings;
}
}
}
}
}
var entityManager = ....something...;
entityManager.fetchMetadata(function (metadata) {
return initializeMetadataStore(entityManager.metadataStore, metadata);
});
now the additional metadata is available in the breeze entity type...
var propertyDisplayName = myEntity.entityType.getProperty('lastName').displayName;
var manager = configureBreezeManager();
function configureBreezeManager() {
breeze.NamingConvention.camelCase.setAsDefault();
var mgr = new breeze.EntityManager('api/breeze');
model.configureMetadataStore(mgr.metadataStore);
mgr.fetchMetadata(function (metadata) {
return initializeMetadataStore(mgr.metadataStore, metadata);
});
return mgr;
};
function initializeMetadataStore(metadataStore, metadata) {
breeze.NamingConvention.defaultInstance = breeze.NamingConvention.none;
var metadataType, metadataProperty, entityProperty, i, j;
for (i = 0; i < metadata.schema.entityType.length; i++) {
metadataType = metadata.schema.entityType[i];
var entityType = metadataStore.getEntityType(metadataType.name);
for (j = 0; j < metadataType.property.length; j++) {
metadataProperty = metadataType.property[j];
entityProperty = entityType.getProperty(metadataProperty.name);
if (entityProperty) {
if (typeof metadataProperty.displayName !== 'undefined') {
entityProperty.displayName = metadataProperty.displayName;
}
if (typeof metadataProperty.displayOrder !== 'undefined') {
entityProperty.displayOrder = metadataProperty.displayOrder;
}
if (typeof metadataProperty.autoGenerateField !== 'undefined') {
entityProperty.autoGenerateField = metadataProperty.autoGenerateField;
}
if (typeof metadataProperty.allowEmptyStrings !== 'undefined') {
entityProperty.allowEmptyStrings = metadataProperty.allowEmptyStrings;
}
}
}
}
}
var readData = function (entityName, observableResults, showLog) {
if (!entityName || !observableResults)
return null;
var query = new breeze.EntityQuery()
.from(entityName);
return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
}
function readEmployee() {
return breezeDataService.readData("Employees", employees, true).then(function () {
var propertyDisplayName = employees()[0].entityType.getProperty('lastName').displayName;//error displayName undefined
}
}
when I get list of entity by readData function that list (observableResults[0]) have not any displayName but I add displayName and checked it by initializeMetadataStore function is correct
FINALLY!!!!! I found it it because of breeze.NamingConvention.camelCase.setAsDefault();

Replace bookmark contents in Word using OpenXml

I can't find any working code examples for replacing bookmark contents. The code should be able to handle both the case replace empty bookmark and replace bookmark with preexisting content.
For example: If I have this text in a Word document:
"Between the following periods comes Bookmark1.. Between next periods comes Bookmark2.."
and I want to insert the text "BM1" between the first periods, and "BM2" between the next.
After the first replacement run, the replacements are inserted correctly.
But after the next replacement run, all of the text on the line after Bookmark1 gets deleted, and then the replacement for Bookmark2 gets inserted.
This is my c# code:
var doc = WordprocessingDocument.Open(#"file.docx", true);
public static Dictionary<string, wd.BookmarkStart> FindAllBookmarksInWordFile(WordprocessingDocument file)
{
var bookmarkMap = new Dictionary<String, wd.BookmarkStart>();
foreach (var headerPart in file.MainDocumentPart.HeaderParts)
{
foreach (var bookmarkStart in headerPart.RootElement.Descendants<wd.BookmarkStart>())
{
if (!bookmarkStart.Name.ToString().StartsWith("_"))
bookmarkMap[bookmarkStart.Name] = bookmarkStart;
}
}
foreach (var bookmarkStart in file.MainDocumentPart.RootElement.Descendants<wd.BookmarkStart>())
{
if (!bookmarkStart.Name.ToString().StartsWith("_"))
bookmarkMap[bookmarkStart.Name] = bookmarkStart;
}
return bookmarkMap;
}
/*extension methods*/
public static bool IsEndBookmark(this OpenXmlElement element, BookmarkStart startBookmark)
{
return IsEndBookmark(element as BookmarkEnd, startBookmark);
}
public static bool IsEndBookmark(this BookmarkEnd endBookmark, BookmarkStart startBookmark)
{
if (endBookmark == null)
return false;
return endBookmark.Id.Value == startBookmark.Id.Value;
}
/* end of extension methods */
public static void SetText(BookmarkStart bookmark, string value)
{
RemoveAllTexts(bookmark);
bookmark.Parent.InsertAfter(new Run(new Text(value)), bookmark);
}
private static void RemoveAllTexts(BookmarkStart bookmark)
{
if (bookmark.ColumnFirst != null) return;
var nextSibling = bookmark.NextSibling();
while (nextSibling != null)
{
if (nextSibling.IsEndBookmark(bookmark) || nextSibling.GetType() == typeof(BookmarkStart))
break;
foreach (var item in nextSibling.Descendants<Text>())
{
item.Remove();
}
nextSibling = nextSibling.NextSibling();
}
}
I have looked around a long time for a general solution.
Any help is appreciated! -Victor
Maybe this can help you
first:delete bookmarkContent
second:find bookMark => insert value
public static void InsertTest1(WordprocessingDocument doc, string bookMark, string txt)
{
try
{
RemoveBookMarkContent(doc, bookMark);
MainDocumentPart mainPart = doc.MainDocumentPart;
BookmarkStart bmStart = findBookMarkStart(doc, bookMark);
if (bmStart == null)
{
return;
}
Run run = new Run(new Text(txt));
bmStart.Parent.InsertAfter<Run>(run, bmStart);
}
catch (Exception c)
{
//not Exception
}
}
public static void RemoveBookMarkContent(WordprocessingDocument doc, string bmName)
{
BookmarkStart bmStart = findBookMarkStart(doc, bmName);
BookmarkEnd bmEnd = findBookMarkEnd(doc, bmStart.Id);
while (true)
{
var run = bmStart.NextSibling();
if (run == null)
{
break;
}
if (run is BookmarkEnd && (BookmarkEnd)run == bmEnd)
{
break;
}
run.Remove();
}
}
private static BookmarkStart findBookMarkStart(WordprocessingDocument doc, string bmName)
{
foreach (var footer in doc.MainDocumentPart.FooterParts)
{
foreach (var inst in footer.Footer.Descendants<BookmarkStart>())
{
if (inst.Name == bmName)
{
return inst;
}
}
}
foreach (var header in doc.MainDocumentPart.HeaderParts)
{
foreach (var inst in header.Header.Descendants<BookmarkStart>())
{
if (inst.Name == bmName)
{
return inst;
}
}
}
foreach (var inst in doc.MainDocumentPart.RootElement.Descendants<BookmarkStart>())
{
if (inst is BookmarkStart)
{
if (inst.Name == bmName)
{
return inst;
}
}
}
return null;
}
This code works but not when the bookmark is placed within a field/formtext (a gray box).
private static void SetNewContents(wd.BookmarkStart bookmarkStart, string text)
{
if (bookmarkStart.ColumnFirst != null) return;
var itemsToRemove = new List<OpenXmlElement>();
var nextSibling = bookmarkStart.NextSibling();
while (nextSibling != null)
{
if (IsEndBookmark(nextSibling, bookmarkStart))
break;
if (nextSibling is wd.Run)
itemsToRemove.Add(nextSibling);
nextSibling = nextSibling.NextSibling();
}
foreach (var item in itemsToRemove)
{
item.RemoveAllChildren();
item.Remove();
}
bookmarkStart.Parent.InsertAfter(new wd.Run(new wd.Text(text)), bookmarkStart);
}