entity-framework-4

Entity Framework code first aspnet_Users mapping / joins

丶灬走出姿态 提交于 2019-12-20 04:24:05
问题 I was wondering with Entity Framework 4.1 code first how do you guys handle queries that involve an existing aspnet_Users table? Basically I have a requirement for a query that involves the aspnet_Users so that I can return the username: SELECT t.Prop1, u.Username FROM Table1 t INNER JOIN aspnet_User u ON t.UserId = u.UserId Where t.Prop2 = true Ideally in linq I would like: from t in context.Table1 join u in context.aspnet_Users on t.UserId equals u.UserId where t.Prop2 = true But I'm not

Convert stored procedure result to model object

谁说胖子不能爱 提交于 2019-12-20 03:49:08
问题 I have a stored procedure In my asp.net mvc3 application using Entity Framework: CREATE PROCEDURE dbo.MinMax AS DECLARE @T1 TABLE(MinColorsId int,MAXColorsId int) INSERT @T1 select MIN(ColorsId) as MinColorsId,MAX(ColorsId) as MAXColorsId from DiamondInfoes SELECT * FROM @T1 RETURN In my model I have same properties names as the procedure table result : public class colorModel { [Display(Name = "MinColorsId")] public float MinColorsId { get; set; } [Display(Name = "MaxColorsId")] public float

The model item passed into the dictionary is of type 'ViewModels.SiteModel',

冷暖自知 提交于 2019-12-20 03:23:40
问题 I'm newbie to MVC architecture.When I'm trying to update, its showing error ,Its totally strange but the data is updating. The model item passed into the dictionary is of type 'CMS.Domain.Models.Site', but this dictionary requires a model item of type 'CMS.Web.ViewModels.SiteModel'.' . Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception

Dynamic column name in where clause. Entity Framework

青春壹個敷衍的年華 提交于 2019-12-20 03:18:02
问题 I want to dynamically place column name in my select-query. How I can do this? public IEnumerable MyTable MySelect(string colName, string param) { using (MyEntities db = new MyEntities ()) { var query = from res in db.MyTable where res.colName.Contains(param) select res; return query; } } 回答1: have you tried res.Field<string>(colName) ? public IEnumerable MySelect(string colName, string param) { using (MyEntities db = new MyEntities ()) { var query = from res in db.MyTable.AsEnumerable()

Modifying a property on an entity in Entity Framework causes validation error

删除回忆录丶 提交于 2019-12-20 02:54:58
问题 I am trying to simply load an entity, modify a property and then save it back to the database. var db = new NewsletterContext(); var newsletter = db.Newsletters.Find(x => x.ID==newsletterID); newsletter.SomeProperty = 5; db.SaveChanges(); This causes a validation error as there are some properties on the newsletter object which are required and apparently not loaded when I do a Find() . I can solve this using an Include() for each required property followed by a Where() : var db = new

Date range validation with Entity Framework 4 data annotations

寵の児 提交于 2019-12-20 02:49:22
问题 I am using Entity Framework 4 to provide the model for a ASP.NET MVC3 / Razor2 web application. I am using DataAnnotations to implement validation. I need to limit some dates to the range accepted by the SQL smalldatetime type. My problem is that I can't get the RangeAttribute to work correctly for a date field. The model metadata definition for the field in question is: [Display(ResourceType = typeof(Resources.Patient), Name = "DateOfBirth_Name")] [DisplayFormat(DataFormatString = "{0:d}",

What is the best way to add attributes to auto-generated entities (using VS2010 and EF4)

删除回忆录丶 提交于 2019-12-20 01:59:13
问题 ASP.NET MVC2 has strong support for using attributes on entities (validation, and extending Html helper class and more). If I generated my Model from the Database using VS2010 EF4 Entity Data Model (edmx and it's cs class), And I want to add attributes on some of the entities. what would be the best practice ? how should I cope with updating the model (adding more fields / tables to the database and merging them into the edmx) - will it keep my attributes or generate a new cs file erasing

Unknown column error using Entity Framework and LINQ

旧巷老猫 提交于 2019-12-20 01:46:54
问题 I started with EF yesterday and I am in trouble to transform this simple query into EF sintax Translate: select a.city from offer o, address a, offer_address oa where o.identifier = oa.offeridentifier and a.identifier = oa.addressidentifier group by a.city order by count(*) desc Into: var cities = (from o in db.offer from a in db.address from oa in db.offer_address where (o.identifier == oa.offeridentifier && a.identifier == oa.addressidentifier) group a by a.city into c select new { quantity

Unknown column error using Entity Framework and LINQ

▼魔方 西西 提交于 2019-12-20 01:46:36
问题 I started with EF yesterday and I am in trouble to transform this simple query into EF sintax Translate: select a.city from offer o, address a, offer_address oa where o.identifier = oa.offeridentifier and a.identifier = oa.addressidentifier group by a.city order by count(*) desc Into: var cities = (from o in db.offer from a in db.address from oa in db.offer_address where (o.identifier == oa.offeridentifier && a.identifier == oa.addressidentifier) group a by a.city into c select new { quantity

Entity Framework: how to do correct “Include” on custom type

别说谁变了你拦得住时间么 提交于 2019-12-20 00:56:42
问题 Suppose we have 2 types, mapped to a Database via EF 4. Schedule 1.....1 Visit Also, we have third custom view type public class ScheduleView { public Schedule Schedule { get; set; } public Visit Visit { get; set; } } So we can write the join query var query = Context.Schedule.Join(Context.Visit ,/*Schedule join key definition*/,/*Visit join key definition*/, (scheduleView, visit) => new ScheduleView {Schedule = scheduleView, Visit = visit}) The problem is that I need to load also Patient