entity-framework-4.1

How to build a dynamic FROM clause for a LINQ query?

余生长醉 提交于 2019-12-03 15:57:44
I have a standard LINQ query: var list = from x in SomeDataContext.ViewName where //Rest of where clause select x; I would like to know if it is possible to build a dynamic LINQ query so that i can change the SomeDataContext.ViewName at runtime. I have about 5 different views, all with the basic columns needed to perform the where clause, but with some different column names for each of other views. So is it possible to build up the query so that i can use the different context at runtime, when needed? Example: public void SomeMethod() { var listA = GetList("DataContext.ViewA"); var listB =

Entity Framework Code First - Configuration in another file

你。 提交于 2019-12-03 15:52:03
What is the best way to separate the mapping of tables to entities using the Fluent API so that it is all in a separate class and not inline in the OnModelCreating method? What I am doing currently: public class FooContext : DbContext { // ... protected override OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Foo>().Property( ... ); // ... } } What i want: public class FooContext : DbContext { // ... protected override OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.LoadConfiguration(SomeConfigurationBootstrapperClass); } } How do you do this? I am using C#. You

Get Record ID in Entity Framework after insert

放肆的年华 提交于 2019-12-03 14:59:00
问题 I'm developing an ASP.net application using Entity Framework. I'm using DetailsView to insert data into database. There is a table as Client and its primary key is client_id . client_id is auto generated by database. I need to get auto generated client_id after inserting a record into Client table and assign it to a hidden field for future use. I searched about this and I found lot of solutions. But I don't know how to use them since I'm new to asp.net. I found that Entity Framework

Entity Framework 4.1 code first approach: how to define length of properties

社会主义新天地 提交于 2019-12-03 14:44:58
问题 As the title implies: how is it possible to tell Entity Framework 4.1 in code first approach, that i do want some properties (in particular of type string) to have a length of 256, or nvarchar(max), or... So if this is for example my model public class Book{ public string Title { get; set; } //should be 256 chars public string Description {get;set} //should be nvarchar(max) } how could it be defined? Thanks in advance! 回答1: In EF4.1 RTW default length is nvarchar(max) for SQL Server and

Entity Framework 4.1 Retrieving self referencing data

試著忘記壹切 提交于 2019-12-03 14:23:57
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I am struggling to get my self referencing setup properly. I have a Category class. It must be self referencing to itself. A category can be a parent category when the ParentCategoryId in the table is null. If a category has a ParentCategoryId with a value then it means that it belongs to a parent category. I followed this article on Code Project. Here is my Category class: public class Category { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public string MetaKeywords { get;

Entity Framework 4.1 - Override Entity (DBSet) with Filter

你离开我真会死。 提交于 2019-12-03 13:48:53
问题 I'm trying to do something which should be relatively easy, but i just dont know how to construct it. I have a Generated Entity which I'd like to override by adding a Linq Where statement. Herewith the partial for the Context : public partial class MyEntities: DbContext { public MyEntities() : base("name=MyEntities") { } public DbSet<Assignee> Assignees { get; set; } } I've created a new partial of MyEntities and tried the following public override DbSet<Assignee> Assignees { get { return

Entity Framework 4.1 Code First: Get all Entities with a specific base class

假装没事ソ 提交于 2019-12-03 13:37:55
I have a DbContext with set up different DbSet<T> s with all types that derive from the same base class: public class Foo : Entity { } public class Bar : Entity { } MyDbContext : DbContext { public DbSet<Foo> Foos { get; set; } public DbSet<Bar> Bars { get; set; } } Is it possible to get all entities which have the Entity base class in one query, like: DbContext.Set<Entity>(); // doesn't work I tried to introduce an explicit DbSet<Entity> to the DbContext, but that results in one big table for all entities in the database. Additional question: If this works somehow, what about querying for

Is there a performance difference between Model First and Code First in MS Entity Framework 4.1?

久未见 提交于 2019-12-03 13:34:02
问题 I'm starting a new development and I plan to use Code First in Entity Framework 4.1. I have previously used Model First and found some performance issues around loading context, first calls to SaveChanges() and where Association Fix-up kicks in. Has anyone compared performance of these two techniques - or, at the end of the day are they insignificant? Thanks. 回答1: I believe that there is no difference at all in performance. Code-First, Model-First, Database-First are modelling strategies at

ASP.NET MVC + Entity Framework with Concurrency check

ぃ、小莉子 提交于 2019-12-03 13:02:26
问题 I am trying to implement a application following the sample in this page: http://www.asp.net/entity-framework/tutorials/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application I have a domain class with Timestamp as the concurrency check field: public class PurchaseOrder { [Timestamp] public byte[] Timestamp {get; set;} } In my Edit.aspx I have the Timestamp as hidden field (I am using a view model): <%: Html.HiddenFor(model => model.PurchaseOrder.Timestamp) %> This is my

EF 4.1 EntityType has no key - composite

风格不统一 提交于 2019-12-03 12:57:20
I have just upgraded to the latest EF 4.1 code-first using NuGet and now I am receiving an error regarding my mapping. I have this class public class UserApplication { [Key, Column(Order = 0)] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int UserId { get; set; } [Key, Column(Order = 1)] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int ApplicationId { get; set; } [ForeignKey("ApplicationId")] public virtual Application Application { get; set; } public DateTime SubscribedDate { get; set; } } And I am getting this error: System.Data.Edm.EdmEntityType: : EntityType