entity-framework-4.1

Entity Framework Code First - Foreign Key to non primary key field

限于喜欢 提交于 2019-12-13 12:08:07
问题 I have two tables that look like this: dbo.ReviewType ReviewTypeId INT PRIMARY KEY ShortName CHAR(1) - Unique Index Description dbo.Review ReviewId INT PRIMARY KEY ReviewType_ShortName CHAR(1) - FK to ReviewType ... A Review always has a ReviewType. A ReviewType can be associated with many reviews. I'm having trouble mapping this in Entity Framework using the Code First Fluent API. It seems like it does not like me using a foreign key that doesn't map to the Primary Key. I'm using a foreign

Mvc 3 Scaffolding: the Model passed to the View throws SQL errror

只谈情不闲聊 提交于 2019-12-13 08:41:33
问题 This post has been heavily edited and updated! The Intent: I am writing an app that is essentially a mini ASP.NET MVC 3 accounting package. I am doing it to learn EF 4.1 Code First and Scaffolding The Setup: I am using SQL Server 2008 Express, Visual Studio 2010 SP1 and ASP.NET MVC 3 with Mvc Scaffolding 1.0.2. I have an existing database. The database has the following tables: Accounts Banks CostCentres Currencies DebitCredits People Transactions TransactionTypes There are a number of

EF 4.1 Code First MVC3 App and Facebook Share Feature

≯℡__Kan透↙ 提交于 2019-12-13 07:28:54
问题 I am working on a simple web application made using Entity Framework 4.1 Code First approach and MVC3. The web application is a simple Quotes website, and on its homepage, the user get a list of quotes. What I would like to do is have a Facebook Share button, like so: And when a user clicks that button, I want the Quote author image, along with his quote and his name to be posted on the user's Facebook wall . I believe they call this Share Functionality. 回答1: Sharing Rich Media https:/

Where Clause With Null Object Using Entity Framework v5.0RC

£可爱£侵袭症+ 提交于 2019-12-13 07:01:51
问题 I was trying to do the following public IList<Category> GetMainCategories() { return _context.Category .Where(x => x.ParentCategory == null) .OrderBy(x => x.SortOrder) .ToList(); } However, no matter what I try this returns no results even though I can see in the collection that ALL ParentCategory's are null? I have read EF has problems with nulls like this and have also tried .Where(x => x.ParentCategory.Equals(null)) And .Where(x => Equals(x.ParentCategory.Id, null)) .Where(x => Equals(x

TryUpdateModel Error

房东的猫 提交于 2019-12-13 05:03:03
问题 First time I can add Allergies into my DB without a problem is As below screen. But when I try to add 2nd record (after save the first record) then it gives below mentioned run time exception (is as below screen). Run time Exception An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving

MVC dbContext find parent record when current record has no elements

ε祈祈猫儿з 提交于 2019-12-13 04:06:01
问题 I'm a MVC/C# beginner so go easy. I have an existing database where a Customer has zero or many Projects. I've built an ASP.Net project from ADODB Entity data model and dbContext code generators. I have a customer: Joe Bloggs (ID=7). I click on the 'Projects' link for Joe Bloggs in my form to see his projects. He doesn't have any. I want to create a project for him so I call the Create action on the project controller. I need to pass Joe Bloggs ID to the Create action for three reasons (which

Adding POCO Entities

旧城冷巷雨未停 提交于 2019-12-13 03:56:52
问题 I have an EDMX file with context.tt and .tt file. I have POCO classes under .tt file. I have one more EDMX with POCO files. I want to copy some POCO classes from 2nd EDMX to 1st EDMX. Please keep in mind that the tables are different in both the EDMX. Just wanted to find out if its possible to copy one POCO class from EDMX and .tt file to another. Any help would be appreciated. 回答1: Solution was easy. Change the connectionstring of EDMX file. In my case I had to create a 2 Function imports

Entity Framework 4.1 Code First - Controlling Eager Loading

为君一笑 提交于 2019-12-13 02:27:24
问题 Is it possible to control eager loading of child objects. If I had a parent class that has 20,000 child objects and I only wanted to retrieve a subset of those child objects is it possible? How would I write the query to do that if it is? For Example: I have an Entity called Book which has a number of related Reviews: public class Book { public int BookId { get; set; } public string BookName { get; set; } public ICollection<Review> Reviews { get; set; } } public class Review { public int

Entity Framework 4.1, MVC3 JsonResult and Circular References

一笑奈何 提交于 2019-12-13 02:18:08
问题 I'm trying to learn Entity Framework Code First development with ASP.NET MVC3. Let's say I have a simple data Model for an Auction and Bids and I'd like to query all the Auctions and their Bids. I have turned off LazyLoadingEnabled and ProxyCreationEnabled. Here is the code I have: public class MiCoreDb2Context : DbContext { public MiCoreDb2Context() : base() { this.Configuration.LazyLoadingEnabled = false; this.Configuration.ProxyCreationEnabled = false; } public DbSet<Auction> Auctions {

Entity Framework Code First primitive collections

旧时模样 提交于 2019-12-12 21:16:13
问题 Given the following simple scenario, what is the best/simpleset way to have a simple collection of primitives persisted? public class Subscriber { public int Id { get; set; } public string Email { get; set; } public ICollection<int> SubscribedNodeIds { get; set; } } If I execute the above example the SubscribedNodeIds column is ignored. 回答1: The obvious answer is to create relationship like so: public class Subscriber { public int Id { get; set; } public string Email { get; set; } public