DbContext remove parent object Id - .net-core

I've got quite weird problem... Here is what it is... When I'm comparing data from somoe goverment service and checking that last born child in my base is the same like in data from service, after updating parent lastBornChild is loosing connection to parent id and it's updated in db wiht ParentId as NULL.
I know that this code should be written better but I wonder why ef changing my lastBornChild.ParentId to NULL.
public class MyService : IMyService
{
MyService(DataDbContext context)
{
_context = context;
}
private DateTime FindYoungestChild(int parentId)
{
var lastChild = _context.Childs.Where(i => i.Parent.Id == parentId).OrderByDescending(d => d.BirthDate).FirstOrDefault();
if (lastChild == null)
return DateTime.MinValue;
else
return lastChild .BirthDate;
}
public Parent UpdateFamilyHistory(Parent parent)
{
DateTime lastStatusTransDate = FindYoungestChild(parent.Id);
var childs= new List<Child>();
var updateChildList = <some goverment public service response>(d => d.DateTimeCreated > lastStatusTransDate).ToList();
foreach (var item in updateChildList)
{
var newChild = new Child
{
Name = item.Name,
BirthDate = item.BirthDate;
};
childs.Add(newChild );
}
parent.Childs = childs;
_context.Update(parent);
_context.SaveChanges();
return parent;
}
}

Try this:
var childs = new List<Child>();
foreach (var item in updateChildList)
{
var newChild = new Child
{
ParentId = parent.Id,
Name = item.Name,
BirthDate = item.BirthDate
};
childs.Add(newChild);
}
_context.Childs.AddRange(childs);
_context.SaveChanges();

Related

SaveChangesAsync() getting stuck

I have problem and i think its a deadlock and i cant resolve the problem. I know there simular problem discuse but i need help. When i try to saveChange on productReposotory it stuck and dont execute the other code.
Here is my controller:
public IActionResult All(string type)
{
var viewModel = new AllFireplaceViewModel
{
Fireplaces =
this.fireplaceService.GetAllFireplaceAsync<IndexFireplaceViewModel>(type).Where(x => x.TypeOfChamber == type),
};
return this.View(viewModel);
}
My IFireplaceService:
using System.Collections.Generic;
using System.Threading.Tasks;
using KaminiCenter.Web.ViewModels.Fireplace;
public interface IFireplaceService
{
Task AddFireplaceAsync(FireplaceInputModel fireplaceInputModel);
IEnumerable<T> GetAllFireplaceAsync<T>(string type);
T GetByName<T>(string name);
}
And this is my implementation of the interface:
public async Task AddFireplaceAsync(FireplaceInputModel model)
{
var typeOfChamber = Enum.Parse<TypeOfChamber>(model.TypeOfChamber);
if (model.Power == null ||
model.Size == null ||
model.Chimney == null)
{
throw new ArgumentNullException("Cannot safe null or whitespace values!");
}
await this.productService.AddProductAsync(model.Name, model.Group);
var productId = this.productService.GetIdByNameAndGroup(model.Name, model.Group);
var groupId = this.groupService.FindByGroupName(model.Group).Id;
var fireplace = new Fireplace_chamber
{
Id = Guid.NewGuid().ToString(),
Power = model.Power,
Size = model.Size,
Chimney = model.Chimney,
Price = model.Price,
Description = model.Description,
ImagePath = model.ImagePath.ToString(),
TypeOfChamber = typeOfChamber,
ProductId = productId,
GroupId = groupId,
CreatedOn = DateTime.UtcNow,
ModifiedOn = DateTime.UtcNow,
};
await this.fireplaceRepository.AddAsync(fireplace);
await this.fireplaceRepository.SaveChangesAsync();
}
This is the productService.AddProductAsync:
public async Task AddProductAsync(string name, string groupName)
{
var group = this.groupService.FindByGroupName(groupName);
if (group == null)
{
await this.groupService.CreateAsync(groupName);
}
var product = new Product
{
// TO Check for Id Initializesion
Id = Guid.NewGuid().ToString(),
Name = name,
GroupId = group.Id,
CreatedOn = DateTime.UtcNow,
ModifiedOn = DateTime.UtcNow,
};
await this.productRepository.AddAsync(product);
await this.productRepository.SaveChangesAsync();
}
And my Add Action
public IActionResult Add()
{
var createFireplaceInputModel = new FireplaceInputModel();
return this.View(createFireplaceInputModel);
}

Get Added Entity State Primary Key Asp.Net Entity Framework

I am trying to create Change Tracker by overriding dbcontext SaveChanges() method.
I couldnt get primary key for entity when state of added.
How can we get added entity state when try to save ChangeLog.
DbContext overrided SaveChanges is like this:
public override int SaveChanges()
{
var adddedEntites = ChangeTracker.Entries().Where(p => p.State == EntityState.Added).ToList();
var now = DateTime.UtcNow;
foreach (var added in adddedEntites)
{
var entityName = added.Entity.GetType().Name;
foreach (var prop in added.CurrentValues.PropertyNames)
{
var currentValue = added.CurrentValues[prop].ToString();
var id = new Guid();
ChangeLog log = new ChangeLog()
{
Id = id,
EntityName = entityName,
PrimaryKeyValue = null// how can I get pkey,
PropertyName = prop,
NewValue = currentValue,
DateChanged = now,
ActionType = "A"
};
ChangeLogs.Add(log);
}
}
return base.SaveChanges()
}
Thanks.
As the primary key is set in base.SaveChanges, you can get it after the call to thereof, e.g. (using reflection):
public override int SaveChanges()
{
var addedEntities = ChangeTracker.Entries().Where(p => p.State == EntityState.Added).ToList();
var result = base.SaveChanges();
foreach (var added in addedEntities)
{
var idInfo = added.Entity.GetType().GetProperty("Id");
var id = idInfo.GetValue(added.Entity);
}
return result;
}

How to get MemberInfo's MemberInfo?

the class is here:
public partial class CodeMergeNodeHandler
{
[State]
public enum State
{
[Finish]
[Label("合并成功")]
Accepted,
[Start]
[Label("创建成功")]
Created,
}
}
I did had got the MemberInfo of the state:
var members = CodeMergeNodeHandler.GetType().GetMembers();
MemberInfo memberOfState ; // the MemberInfo of the Enum State;
foreach (var member in members)
{
var attrs = member.GetCustomAttributes();
if (attrs.Any(attr => attr is StateAttribute))
{
memberOfState = member;
}
}
I try to get the members of the memberOfState ,the Accepted and the Created:
private static (string start, string end) GetStartEnd(this MemberInfo member)
{
var start = default(string);
var end = default(string);
var members = member.GetMembers(); //error, here is my question.
foreach (var sub in members)
{
var attrs = sub.GetCustomAttributes();
if (attrs.Any(attr => attr.GetType() == typeof(StartAttribute)))
{
start = sub.ToString();
}
if (attrs.Any(attr => attr.GetType() == typeof(FinishAttribute)))
{
end = sub.ToString();
}
}
return (start, end);
}
Why member have no Method as GetMembers(), but when I Debug like the picture, the target is in the DeclaredMembers , and How I can get them ??
enter image description here
I solved the question:
Type type = member as Type;
Make the MemberInfo class as the Type class, you can use Methods of Type class.

Web API Typeless OData Service with OWIN self-hosting returns 406 Not Acceptable

I'm trying to set up a Web API Typeless OData Service with OWIN self-hosting... =)
But why not working? :~(
This is some code I have partially extracted from all kinds of examples out there...
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
appBuilder.UseWebApi(config);
}
}
public class Program
{
public static IEdmModel Model = GetEdmModel();
static void Main(string[] args)
{
using (WebApp.Start<Startup>("http://localhost:8080"))
{
Console.WriteLine("Running...");
Console.ReadLine();
}
}
public static IEdmModel GetEdmModel()
{
var model = new EdmModel();
// Create and add product entity type.
var product = new EdmEntityType("NS", "Product");
product.AddKeys(product.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
product.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
product.AddStructuralProperty("Price", EdmPrimitiveTypeKind.Double);
model.AddElement(product);
// Create and add category entity type.
var category = new EdmEntityType("NS", "Category");
category.AddKeys(category.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
category.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
model.AddElement(category);
// Set navigation from product to category.
var propertyInfo = new EdmNavigationPropertyInfo();
propertyInfo.Name = "Category";
propertyInfo.TargetMultiplicity = EdmMultiplicity.One;
propertyInfo.Target = category;
var productCategory = product.AddUnidirectionalNavigation(propertyInfo);
// Create and add entity container.
var container = new EdmEntityContainer("NS", "DefaultContainer");
model.AddElement(container);
// Create and add entity set for product and category.
var products = container.AddEntitySet("Products", product);
var categories = container.AddEntitySet("Categories", category);
products.AddNavigationTarget(productCategory, categories);
return model;
}
}
public class ProductsController : ODataController
{
private static readonly IQueryable<IEdmEntityObject> Products = Enumerable.Range(0, 10).Select(i =>
{
var productType = (IEdmEntityType)Program.Model.FindType("NS.Product");
var categoryType = (IEdmEntityTypeReference)productType.FindProperty("Category").Type;
var product = new EdmEntityObject(productType);
product.TrySetPropertyValue("Id", i);
product.TrySetPropertyValue("Name", "Product " + i);
product.TrySetPropertyValue("Price", i + 0.01);
var category = new EdmEntityObject(categoryType);
category.TrySetPropertyValue("Id", i % 5);
category.TrySetPropertyValue("Name", "Category " + (i % 5));
product.TrySetPropertyValue("Category", category);
return product;
}).AsQueryable();
public EdmEntityObjectCollection Get()
{
// Get Edm type from request.
var path = this.Request.GetODataPath();
var edmType = path.EdmType;
Contract.Assert(edmType.TypeKind == EdmTypeKind.Collection);
var collectionType = edmType as IEdmCollectionType;
var entityType = collectionType.ElementType.Definition as IEdmEntityType;
var model = Request.GetEdmModel();
var queryContext = new ODataQueryContext(model, entityType);
var queryOptions = new ODataQueryOptions(queryContext, Request);
// Apply the query option on the IQueryable here.
return new EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType, false), Products.ToList());
}
public IEdmEntityObject GetProduct(int key)
{
object id;
var product = Products.Single(p => HasId(p, key));
return product;
}
public IEdmEntityObject GetCategoryFromProduct(int key)
{
object id;
var product = Products.Single(p => HasId(p, key));
object category;
if (product.TryGetPropertyValue("Category", out category))
{
return (IEdmEntityObject)category;
}
return null;
}
public IEdmEntityObject Post(IEdmEntityObject entity)
{
// Get Edm type from request.
var path = Request.GetODataPath();
var edmType = path.EdmType;
Contract.Assert(edmType.TypeKind == EdmTypeKind.Collection);
var entityType = (edmType as IEdmCollectionType).ElementType.AsEntity();
// Do something with the entity object here.
return entity;
}
private bool HasId(IEdmEntityObject product, int key)
{
object id;
return product.TryGetPropertyValue("Id", out id) && (int)id == key;
}
}
The result I get is:
{StatusCode: 406, ReasonPhrase: 'Not Acceptable', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Mon, 12 May 2014 18:08:25 GMT
Server: Microsoft-HTTPAPI/2.0
Content-Length: 0
}}
From running this:
var client = new HttpClient();
var response = client.GetAsync("http://localhost:8080/api/Products").Result;
If you are using OData V4, you need to make a change in your controller:
Old:
using System.Web.Http.OData;
New:
using System.Web.OData;

How to map lists with ValueInjector

I am using ASP.NET MVC 3.
Can someone please help me clarify what's happening here:
var person = new PersonRepository().Get();
var personViewModel = new PersonViewModel();
personViewModel.InjectFrom<LoopValueInjection>(person)
.InjectFrom<CountryToLookup>(person);
I have a grid on my Index view. Each row is an instance of a CategoryViewModel. So what I do is to get a list of all the categories and then map each Category to a CategoryViewModel, and then pass this list of CategoryViewModels to the view. Hou would I do a mapping like that?
IEnumerable<Category> categoryList = categoryService.GetAll();
I thought the following would work but it doesn't:
// Mapping
IList<CategoryViewModel> viewModelList = new List<CategoryViewModel>();
viewModelList.InjectFrom(categoryList);
AFAIK value injecter doesn't support automatic collection mapping like AutoMapper but you could use a simple LINQ expression and operate on each element:
IEnumerable<Category> categoryList = categoryService.GetAll();
IList<CategoryViewModel> viewModelList = categoryList
.Select(x => new CategoryViewModel().InjectFrom(x)).Cast<CategoryViewModel>()
.ToList();
//source list
IEnumerable<string> items = new string[] { "1", "2" };
// target list
List<int> converted = new List<int>();
// inject all
converted.InjectFrom(items);
And the extension method:
public static ICollection<TTo> InjectFrom<TFrom, TTo>(this ICollection<TTo> to, IEnumerable<TFrom> from) where TTo : new()
{
foreach (var source in from)
{
var target = new TTo();
target.InjectFrom(source);
to.Add(target);
}
return to;
}
ICollection<T> is the interface that got least features but a Add method.
Update
An example using more proper models:
var persons = new PersonRepository().GetAll();
var personViewModels = new List<PersonViewModel>();
personViewModels.InjectFrom(persons);
Update - Inject from different sources
public static ICollection<TTo> InjectFrom<TFrom, TTo>(this ICollection<TTo> to, params IEnumerable<TFrom>[] sources) where TTo : new()
{
foreach (var from in sources)
{
foreach (var source in from)
{
var target = new TTo();
target.InjectFrom(source);
to.Add(target);
}
}
return to;
}
Usage:
var activeUsers = new PersonRepository().GetActive();
var lockedUsers = new PersonRepository().GetLocked();
var personViewModels = new List<PersonViewModel>();
personViewModels.InjectFrom(activeUsers, lockedUsers);
Use this function definition
public static object InjectCompleteFrom(this object target, object source)
{
if (target.GetType().IsGenericType &&
target.GetType().GetGenericTypeDefinition() != null &&
target.GetType().GetGenericTypeDefinition().GetInterfaces() != null &&
target.GetType().GetGenericTypeDefinition().GetInterfaces()
.Contains(typeof(IEnumerable)) &&
source.GetType().IsGenericType &&
source.GetType().GetGenericTypeDefinition() != null &&
source.GetType().GetGenericTypeDefinition().GetInterfaces() != null &&
source.GetType().GetGenericTypeDefinition().GetInterfaces()
.Contains(typeof(IEnumerable)))
{
var t = target.GetType().GetGenericArguments()[0];
var tlist = typeof(List<>).MakeGenericType(t);
var addMethod = tlist.GetMethod("Add");
foreach (var sourceItem in source as IEnumerable)
{
var e = Activator.CreateInstance(t).InjectFrom<CloneInjection>(sourceItem);
addMethod.Invoke(target, new[] { e });
}
return target;
}
else
{
return target.InjectFrom(source);
}
}
For those like me who prefer shortest notations possible
public static ICollection<TTarget> InjectFromList<TTarget, TOrig>(this ICollection<TTarget> target, ICollection<TOrig> source) where TTarget : new()
{
source.Select(r => new TTarget().InjectFrom(r))
.Cast<TTarget>().ToList().ForEach(e => target.Add(e));
return target;
}
public static ICollection<TTarget> InjectFromList<TTarget, TOrig>(this ICollection<TTarget> target, params ICollection<TOrig>[] sources) where TTarget : new()
{
sources.ToList().ForEach(s => s.ToList().Select(r => new TTarget().InjectFrom(r))
.Cast<TTarget>().ToList().ForEach(e => target.Add(e)));
return target;
}
Create a generic list mapper:
public class ValueMapper
{
public static TResult Map<TResult>(object item) where TResult : class
{
return item == null ? null : Mapper.Map<TResult>(item);
}
public static IEnumerable<TResult> MapList<TResult>(IEnumerable<object> items) where TResult : class
{
return items?.Select(i => Mapper.Map<TResult>(i));
}
}
Now you can reference the ValueMapper class wherever you want, and call both Map and MapList
var mydtos = ValueMapper.MapList<MyDto>(dtos);
var mydto = ValueMapper.Map<MyDto>(dto);

Resources