entity-framework

EF. How to include only some sub results in a model?

邮差的信 提交于 2020-01-21 21:01:06
问题 I'm trying to select list of Users and for each User JobTitle in correct language depended of strLang selected by user. Something like that: IList<User> myData; myData = Context.Users.Where(u => u.Location == strLocation) .Include(u => u.JobTitles.Where(e => e.Language == strLang)) .ToList(); But it seems Include doesn't like Where clause 回答1: You can't conditionally include only a few entities of a related collection, so you should use projection to get the stuff you need: IList<User> myData

Generic class with Id c#

ⅰ亾dé卋堺 提交于 2020-01-21 17:55:31
问题 I am not sure if this is possible, but I have started a new project and I am trying to clean up the way I work with the DbContext . I am trying to do this by using generics. The first part is easy. I create a Repository class like this: public class Repository<T> where T : class { protected readonly DbContext _dbContext; protected readonly DbSet<T> _dbEntitySet; public Repository(InteractiveChoicesContext dbContext) { _dbContext = dbContext; _dbEntitySet = dbContext.Set<T>(); } /// <summary>

Error Message: “Only primitive types or enumeration types are supported in this context.”

限于喜欢 提交于 2020-01-21 16:36:27
问题 I'm working a report for an asp.net page and I'm trying to assign the results of this linq statement to an existing dataset but I'm receiving the "Only primitive types or enumeration types are supported in this context." Error message. Here's my code: var contextTable = new dbpersonnelEntities(); var contextHistory = new WriterInResidenceEntities(); var rslt = (from c in contextTable.TableofOrganizationRpts where !(from o in contextHistory.emp_history select o.employeeID).Contains((int)c

Entity Framework 7 IDENTITY_INSERT

荒凉一梦 提交于 2020-01-21 13:25:39
问题 I need to store a large amount of user data in a database where every user already has an ID as a key. However, these users all out of a larger pool of users, therefore, the ID does not increment and cannot be automatically generated. I need to be able to manually set the ID. Is there an easy way to do this? 回答1: I've made some tests and this seems to work for me on EF 7.0.0 rc1-final: modelBuilder.Entity<Foo>().HasKey(x => x.Id); modelBuilder.Entity<Foo>().Property(x => x.Id)

Error Using ADO.NET Entity Framework

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-21 11:45:14
问题 I want to convert a list to EntityCollection. List<T> x = methodcall(); EntityCOllection<T> y = new EntityCollection<T>(); foreach(T t in x) y.Add(t); I get this error. The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object. Anyone know about this error? 回答1: It sounds like x is the result of an ObjectContext query. Each

Entity Framework Core: Include many-to-many related objects in WebAPI

試著忘記壹切 提交于 2020-01-21 09:07:07
问题 I'm not too familiar with the .NET framework but decided to try out ASP.NET Core and EF Core. I want to make a pretty simple Web API backend but I'm having trouble working with many-to-many relationships. I understand that I need to make a relationship table for the two entities, as in the example from this post: How to create a many to many relationship with latest nightly builds of EF Core? I also have my model builder creating the relationship as described here http://ef.readthedocs.io/en

Entity framework metadata exception - No csdl, ssdl, msl in dll resources

你。 提交于 2020-01-21 07:49:47
问题 I have DAL (model first entity framework 4.1) and Service which is using it in separate projects. Everything was working fine, but after some minor changes (for example I generated model from database) it stoppedd working. I am now getting metadata exception. After many hours of research I downloaded ILSpy and checked that inside DAL.dll there are no resources. My connection string looks like: metadata=res://*/DataModel.TerminalRegistryModel.csdl| res://*/DataModel.TerminalRegistryModel.ssdl|

Identity specification set to false

六眼飞鱼酱① 提交于 2020-01-21 06:51:00
问题 I am using EF5 and Code First to create database. When Entity has Id field , EF create such field as Primary Key in database and set Identity specification to true (auto generated value). How to set Identity specification to false by default ? 回答1: If you don't want to use identity keys you have several options. Option 1: You can globally turn off this feature by removing StoreGeneratedIdentityKeyConvention : public class YourContext : DbContext { protected override void OnModelCreating

Can I use custom delegate method in Where method of Entity Framework?

我的未来我决定 提交于 2020-01-21 05:22:06
问题 Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate); I pass parameter to Where method as follows: f => f.Id > 4 . Can I pass a delegate method instead of f.Id > 4 ? 回答1: No. The Entity Framework needs to be able to see everything that is being attempted. So if you simply did something like this: queryable.Where(f => DelegateFunc(f)); Where the definition of DelegateFunc looks like this: public bool DelegateFunc(Foo foo) { return foo.Id > 4; } The Entity

EF Core (1.0.0) Migrations On Azure App Services

陌路散爱 提交于 2020-01-21 04:57:05
问题 We have a web app (Net Core 1.0.0-preview2-003121) deployed to an Azure App Service and we're struggling to deploy the migrations. In RC1/2 it was possible to do the migrations with an ef.cmd file that seemed to be automagically there, namely we could use this file to run dnx ef database update but this is gone. dotnet ef is not installed in the Azure App Service itself, so this is not an option. Any ideas that don't involve running the migrations from code/deploying them from visual studio?