entity-framework-4

EF: select all entities of subclass (inheritance)

梦想与她 提交于 2019-12-11 07:16:36
问题 I am using the Database first approach and DbContext. I have several inheritance structures (TPH) in my data model. But DbContext only creates one DbSet for the base class, and none for the subclasses. How should I retrieve all Entities of a specified subclass? I cannot write queries based on the mapping criteria, as i had to remove those fields from the data model. Can I simply add a new DbSet to the Entities class (partial class) ? 回答1: You can add new properties returning DbSet s of your

How to define common property(ies) for all entities in entity framework

风格不统一 提交于 2019-12-11 07:12:30
问题 Imagine that I have UserId (actually we do have roughly four columns like userId, adddate, moddate etc. for almost all tables) column for almost all of the entities. How can I make such a column(s) to be available in a separate entity (say parent entity) and make all other entities (child entities) inheriting from it? 回答1: If you are using generated entity classes (either POCO or EntityObject ) you will have to define base abstract entity in your model (EDMX) and derive other entities in TPC

Exposing EntityFramework 4 entities as IList instead of IObjectSet

依然范特西╮ 提交于 2019-12-11 07:08:45
问题 I have a 'Customer' POCO entity within my Entity Framework 4 project. I want to expose my Customer entities to my upper layers as a generic list rather than an ObjectSet. I have an IUnitOfWork interface which looks as follows: public interface IUnitOfWork { string Save(); IList<Customer> Customers { get; } } Down at my Entity Framework DAL (which implements the above interface) I have the following: public class EntityContainer : ObjectContext, IUnitOfWork { private IObjectSet<Customer>

How do I get Age distribution list by grouping

纵饮孤独 提交于 2019-12-11 07:07:14
问题 I want to return a List which contains data related to the Age Distribution (Eg. For Age 0-9 there are 10 persons, for 10-19 there are 7 persons etc) This is my query :- public List<ReportHelper> GetAgeDistribution(int userId) { var itemList = (from item in this.ObjectContext.TreeMembers where item.UserId == userId group (DateTime.Now - (item.Birthdate ?? DateTime.Now)).Days/365.25 by ((DateTime.Now - (item.Birthdate ?? DateTime.Now)).Days / 365.25) / 9); List<ReportHelper> list = new List

Load operation failed for query due to data limit

独自空忆成欢 提交于 2019-12-11 07:04:37
问题 We have a silverlight application which interfaces with an RIA service to get a list of contacts to display in a grid, this normally works however we are getting the following error: load operation failed for query x. the remote server returned an error: notfound Tracing this through we have determined it is due to the amount of data that is being passed as it will work if the we pass roughly 3,800 records or less. We need to load at least 15,000 records from the database. I have searched all

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily

风流意气都作罢 提交于 2019-12-11 07:00:25
问题 I am Using Entity Framework 4.0 calling the below mentioned code from asp.net. I have a address table in which I need to insert some data. my code is : IRepository<Address> addressRepository; int addressHOCODE = 0; try { **addressRepository = ObjectFactory.GetInstance<IRepository<Address>>();** addressRepository.Add(address); addressRepository.SaveChanges(); addressHOCODE = address.HOCODE; } catch ... At the addressRepository = ObjectFactory.GetInstance<IRepository<Address>>(); line, we're

Writing a SQLdatasource in code behind in C# to use a code behind value

六眼飞鱼酱① 提交于 2019-12-11 06:59:18
问题 I am trying to populate a data table using the SQL datasource based upon parametrized inputs .The tricky thing is that the value of the parametrized input is available only in the code behind so I am forced to write the SQL datasource in the code behind page How do I go about it ( I am using C# and ASP.Net 4.0) The current code in the asp.net page to create the sql datasource connection is : <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings

Entity Framework and (1 to many)-(many to 1) (1 - * * - 1) relations

依然范特西╮ 提交于 2019-12-11 06:49:58
问题 Iam have some trouble for some time to figure out how to take data from a data table (MSSQL 2008). Problem is this. You have 3 tables: TABLE1 (Jobs): JobID, JobName TABLE2 (Worker): WorkerID, WorkerName TABLE3 (Worker2Job): RowID, WorkerID, JobID I Assume that JOB can be done by many workers, so I need "Worker2Job" table. So I can type that JobID:1 is made by WorkerID1 and WorkerId2 etc... Now using Entity framework I dont know how to fetch the "WorkerName" property for the first of worker

Two foreign keys with same Navigation Property?

ε祈祈猫儿з 提交于 2019-12-11 06:46:33
问题 I am new to Entity Framework so I don't know much about it. Currently I am working on My College Project, in that Project I came across a problem where I have two foreign keys refers to the Same column in another table. how can I handle this situation. Is it necessary to create Navigation Property for Every Foreign key. And if I create another Navigaton property for ContactId then it is necessary to create another Navigation Property in User class like: public virtual ICollection<BlockedUser>

Update object on saving

≯℡__Kan透↙ 提交于 2019-12-11 06:45:46
问题 I have an entity class (using model first, if that matters) that should persist its own last change date. Is there a way to update this on saving? Right now I have only found overriding OnPropertyChanged but that is not the right override, as it is also called when loading the object. 回答1: You can override SaveChanges on your ObjectContext derived class and modify the last change date just before saving. Something like: public override int SaveChagnes(SaveOptions saveOptions) { var entities =