entity-framework-4

EF4 LINQ Include(string) alternative to hard-coded string?

邮差的信 提交于 2019-12-18 11:53:07
问题 Is there any alternative to this: Organizations.Include("Assets").Where(o => o.Id == id).Single() I would like to see something like: Organizations.Include(o => o.Assets).Where(o => o.Id == id).Single() to avoid the hard-coded string "Assets". 回答1: For Entity Framework 1.0, I created some extensions methods for doing this. public static class EntityFrameworkIncludeExtension { public static ObjectQuery<T> Include<T>(this ObjectQuery<T> src, Expression<Func<T, StructuralObject>> fetch) { return

Using stored procedures with Entity Framework 4 Code First CTP5

一笑奈何 提交于 2019-12-18 11:32:05
问题 I am using Entity Framework 4 Code First CTP5 and ASP.NET MVC 3. I would like to know how do I make use of stored procedures? I have the following code in my respository class: MyDatabaseContext db = new MyDatabaseContext(); public void Insert(News news) { db.Newses.Add(news); db.SaveChanges(); } And then my insert store procedure will look like this: ALTER PROCEDURE [dbo].[News_Insert] ( @Title VARCHAR(100), @Body VARCHAR(MAX), @NewsStatusId INT ) AS BEGIN INSERT INTO News ( Title, Body,

How do I set Identity seed on an ID column using Entity Framework 4 code first with SQL Compact 4?

痴心易碎 提交于 2019-12-18 09:15:33
问题 I am having some problem setting the Identity Seed on the Id column in SQL Compact 4 using the code first approach. I have tried this context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Members', RESEED, 100001"); but this is not working in Sql Compact. MyDbContext: protected override void OnModelCreating(DbModelBuilder modelBuilder) { SetupMemberEntity(modelBuilder); } private static void SetupMemberEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<Member>().Property(m => m.Id); //

Cannot convert type IEnumerable to ObservableCollection…are you missing a cast?

笑着哭i 提交于 2019-12-18 09:14:10
问题 I'm trying to return entities where the bool "isAssy" is true: public ObservableCollection<MasterPartsList> ParentAssemblyBOM { get {return this._parentAssemblyBOM.Where(parent => parent.isAssy == true); } } but the entire statement is underlined in red stating that I cannot "convert type IEnumerable to ObservableCollection...are you missing a cast?" 回答1: ObservableCollection<T> has an overloaded constructor that accepts an IEnumerable<T> as a parameter. Assuming that your Linq statement

How can l use Entity Framework without App.config

末鹿安然 提交于 2019-12-18 08:57:43
问题 I want to use Entity Framework without app.config file. I want to define a string variable Connection String in my code and use that to connect to the database. Please show me the way if it is possible. 回答1: You're not mentioning what approach you're using (database-first, model-first, code-first) - but basically, in the end, you need to define a string variable and assign it a valid EF connection string string myConnectionString = "...(define a valid EF connection string here)......";

When is ObjectQuery really an IOrderedQueryable?

那年仲夏 提交于 2019-12-18 08:56:41
问题 Applied to entity framework, the extension methods Select() and OrderBy() both return an ObjectQuery , which is defined as: public class ObjectQuery<T> : ObjectQuery, IOrderedQueryable<T>, IQueryable<T>, <... more interfaces> The return type of Select() is IQueryable<T> and that of OrderBy is IOrderedQueryable<T> . So you could say that both return the same type but in a different wrapper. Luckily so, because now we can apply ThenBy after OrderBy was called. Now my problem. Let's say I have

When is ObjectQuery really an IOrderedQueryable?

浪子不回头ぞ 提交于 2019-12-18 08:56:23
问题 Applied to entity framework, the extension methods Select() and OrderBy() both return an ObjectQuery , which is defined as: public class ObjectQuery<T> : ObjectQuery, IOrderedQueryable<T>, IQueryable<T>, <... more interfaces> The return type of Select() is IQueryable<T> and that of OrderBy is IOrderedQueryable<T> . So you could say that both return the same type but in a different wrapper. Luckily so, because now we can apply ThenBy after OrderBy was called. Now my problem. Let's say I have

LINQ to Entities StringConvert(double)' cannot be translated to convert int to string

倖福魔咒の 提交于 2019-12-18 08:56:06
问题 Problem Need to convert int to string using EF4 + SQL CE4. The recommended option of using SqlFunctions.StringConvert(double) still gives me errors. Option 1 (original). This gives me error: public static IEnumerable<SelectListItem> xxGetCustomerList() { using (DatabaseEntities db = new DatabaseEntities()) { var list = from l in db.Customers orderby l.CompanyName select new SelectListItem { Value = l.CustomerID.ToString(), Text = l.CompanyName }; return list.ToList(); } } Option 2 (most

Entity Framework, MVC 3, OrderBy in LINQ To Entities

久未见 提交于 2019-12-18 08:53:24
问题 I've got the following query: model.Page = db.Pages .Where(p => p.PageId == Id) .Include(p => p.Series .Select(c => c.Comics .Select(col => col.Collection))) .SingleOrDefault(); This works great, although I now need to order the Comics by a property called 'ReadingOrder'. I've tried: model.Page = db.Pages .Where(p => p.PageId == Id) .Include(p => p.Series.Select(c => c.Comics.OrderBy(o => o.ReadingOrder) .Select(col => col.Collection))) .SingleOrDefault(); But this results in the following

How to set up a many-to-many relationship in Entity Framework designer thingy

笑着哭i 提交于 2019-12-18 08:14:11
问题 I'm an NHibernate developer trying to give Entity Framework a shot in a hobby project. I'm used to specifying mapping data in code using Fluent NHibernate. Pardoning Microsoft's belief that developers shouldn't be allowed to write code, I'm trying to create my mappings using the Entity Framework's visual designer surface (which you get by opening the .edmx file in Visual Studio). I have no idea how to set up a many-to-many relationship! I have 'updated the model' from the database, but I get