entity-framework

EF5 Code First TransactionScope without escalating to MSDTC

拟墨画扇 提交于 2020-01-05 06:51:18
问题 I'm using EF5 code first, and I need to wrap multiple calls to SaveChanges() in the same transaction. I'm relatively new to using transactionScope, and can't seem to tweak the options on it to accomplish what I'm looking to do. The following code resides in a service class that gets injected with an instance of my DbContext class. I do not create additional instances of DbContext anywhere in this class. At first I tried the following. The code is nice and clean, but it attempts to escalate to

Get distinct values of List in List

好久不见. 提交于 2020-01-05 06:46:25
问题 I'm struggling with a db application, where I need to get the distinct values. The whole structure looks somewhat like this class A { public virtual ICollection<B> Bs { get; set; } } class B { public virtual C C { get; set; } } class C { public int x {get; set;} } This is the model from a db and I have a subset of all A's . Now I need to get all distinct values C.x from all those A's . So basically something like db.A.Where(something).SelectMany(s => s.Bs.SelectMany(t => t.C.x).ToList()

Can't force List<int> to be required

你说的曾经没有我的故事 提交于 2020-01-05 06:07:22
问题 I'm trying to use Data Annotations to add validation to a List in my model that cannot be empty. I've tried several implementations of a custom attribute, including ones here and here. My view: <div class="form-group"> @* Model has a list of ints, LocationIDs *@ @Html.LabelFor(model => model.LocationIDs, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <select class="select2 form-control" multiple id="LocationIDs" name="LocationIDs"> @* Adds every possible

Dynamically Set Connection String in Entity Data Model in C#

本秂侑毒 提交于 2020-01-05 05:54:20
问题 I'm working on a project that relies on the ADO.NET Entity Data Model. I have 4 identical database schemas, one for each department in my company (Marketing, Finanace, Administrative, and HR). I currently know which which department each of my users resides in. I would like to use the department to determine which database the user can connect to. In C# code, how do I set the connection string at runtime? Currently, I have string connectionString = GetUsersConnectionString(); using

Entity framework and Eager loading and enterprise application with DDD aproach

北慕城南 提交于 2020-01-05 05:44:06
问题 ITNOA We are trying to create an ASP.NET MVC 4 application using entity framework with domain driven development style pattern approach. As you can see in our part of domain layer, we have a complex design. We need to load entity with lazy as well as eager methods. Unfortunately we have big problem with these methods in entity framework. As we understand, for eager loading in EF we have to use the Include method and give string of properties and properties of properties etc. (In Hibernate and

Updating primary key in Entity Framework Code First

白昼怎懂夜的黑 提交于 2020-01-05 05:00:12
问题 I want to update a mediate table's primary keys. This post suggests to take another primary key and don't change it, but we're using our tables in other non-EF projects, I didn't designed them and I'm not able to change them. Do I have any other option? Anything? Even deleting the old record and inserting a new one. I just don't know how to retrieve the old values. This is my class: public class ZChangeUnits : User { [Key] [Column(TypeName = "VARCHAR", Order = 0), StringLength(4)] public

Linq “join” with a IList<T> getting “Error Unable to create a constant value..”

让人想犯罪 __ 提交于 2020-01-05 05:00:09
问题 I have a Save Method that saves with a Linq query a manually re-orderd list (in a web form) that is passed as the parameter to my method , and I try to update the Order Property of the IEnumerable<VM_CategoryLabel> I retrieve from the database (EF) with the corresponding value in the list (maybe would that be clearer with my code below): public static void SaveFromList(IList<VM_CategoryLabelExtra> listTemplate) { int idCat = listTemplate.Select(x => x.IdCat).FirstOrDefault(); var test = (int

MVC3 + EF4 + MySQL System.Security.SecurityException: Request failed

て烟熏妆下的殇ゞ 提交于 2020-01-05 04:55:56
问题 I'm developing a ASP.NET MVC3 web application, which uses Entity Framework 4.1 and MySQL. Locally all works well, but when I deploy the solution to the web host, it gives me the following error message: System.Security.SecurityException: Request failed. at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(RuntimeAssembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandleInternal rmh, SecurityAction action, Object demand, IPermission permThatFailed) at System

registering DbContext with multiple parameters

蓝咒 提交于 2020-01-05 04:51:05
问题 I'm trying to inject TenantProvider into DbContext public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long> { public int? _tenantId; public ITenantProvider _tenantProvider; public AppDbContext( DbContextOptions<AppDbContext> options, ITenantProvider tenantProvider ) : base(options) { _tenantProvider = tenantProvider; } but I don't understand how to register it correctly - if I put the breakpoint in the constructor - tenantProvider is null . The bit from Startup.cs

EF Core - How can I retrieve associated entities of an aggregate root?

吃可爱长大的小学妹 提交于 2020-01-05 04:27:12
问题 In my app I am trying to follow DDD whilst modelling a simple fund account. The classes are FundAccount public Guid Id { get; set; } private ICollection<AccountTransaction> _transactions = new List<AccountTransaction>(); public IReadOnlyCollection<AccountTransaction> Transactions => _transactions .OrderByDescending(transaction => transaction.TransactionDateTime).ToList(); AccountTransaction public Guid Id { get; set; } public decimal Amount { get; set; ) I am trying to retrieve the fund