entity-framework-4.1

How to handle an association table linking the same 2 tables

你说的曾经没有我的故事 提交于 2019-12-06 04:48:32
I am using Entity Framework 4.3 code first using an already existing database. There is a Users table that has the following columns: - UserID (int) - FirstName (string) - Surname (string) I have an association table called Impersonations . The Impersonations table is an association table between a user table and the same user table. A user can have a list of users. Here is the Impersonations table structure: - ImpersonateID (int primary key) - UserID (int FK to Users table's UserID column) - ImpersonatedUserID (int FK to Users table's UserID column) I have a User class with properties that I

How do I map to and from a complex type in EF4.3 code-first?

只愿长相守 提交于 2019-12-06 04:14:40
问题 I've got a entity type like this: public class Invoice{ public int Id { get; set; } public InvoiceNumberSequence Sequence { get; set; } public decimal Amount { get; set; } } The InvoiceNumberSequence looks like this: public class InvoiceNumberSequence { public string Prefix { get; set; } public int Number { get; set; } public string GetSequence() { return Prefix + Number; } } My problem is that I have an existing database that I cannot change and I'm trying to map tables/columns to my domain

Does LINQ to Entities support IEquatable in a 'where' clause predicate?

会有一股神秘感。 提交于 2019-12-06 03:01:06
I have a simple base entity type called EntityBase that implements IEquatable<EntityBase>. I've used this code outside of LINQ to Entities (with EF 4.1) to filter a list of types derived from EntityBase using a predicate where clause that compares the object instances rather than the ID property they inherit from EntityBase. So basically I want to do this: UserAccount account = GetMyNewAccount(); Accounts.Where(item => item == account).SingleOrDefault(); rather than this: UserAccount account = GetMyNewAccount(); Accounts.Where(item => item.Id == account.Id).SingleOrDefault(); Unfortunately, EF

What does a repository look like when using many 1:Many or Many:Many tables?

假装没事ソ 提交于 2019-12-06 02:59:33
I have a MVC application that uses that entity framework model object directly in the view. I'm not sure how bad of a design pattern this is, but based on this sample I need to use a repository to implement caching. namespace MVCAzureStore.Services.Caching { public class CachedProductsRepository : CachedDataSource, IProductRepository { private readonly IProductRepository repository; public CachedProductsRepository(IProductRepository repository, ObjectCache cacheProvider) : base(cacheProvider, "Products") { this.repository = repository; } public List<string> GetProducts() { return

Entity Framework 4.1: Override IEnumerable<ValidationResult> Validate

拥有回忆 提交于 2019-12-06 02:23:42
问题 public abstract class Animal , IValidatableObject { public string Id {get;set;} public string Name {get;set;} public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { if (this.Name == "animal") { yield return new ValidationResult("Invalid Name From base", new[] { "Name" }); } } } public class Dog: Animal, IValidatableObject { public string Owner {get;set;} public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { /* Here

LINQ to Entities ToString() - None of the proposed solutions work?

♀尐吖头ヾ 提交于 2019-12-06 01:54:27
问题 I'm posting this more because I'd like to learn more, because my work-around has been to basically avoid using LINQ to Entities! It would nice if I could use it though. So if anyone can enlighten me.. I'm working on an ASP.Net MVC 3 application (my first one) using code-first Entity Framework as my data model. It's going to be an online shop and I am working on the 'Admin' controller where, as an admin, you will be able to edit products etc. When it comes to adding a new product, you must

ASP.Net MVC 3 EF “Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths”

夙愿已清 提交于 2019-12-06 01:46:29
I am creating an ASP.Net MVC 3 application and I am running into a foreign key constraint problem when trying to update my database using migrations. I am using Code-First, and the error I am getting is: Introducing FOREIGN KEY constraint 'FK_CategoryItemValues_CategoryProperties_CategoryPropertyId' on table 'CategoryItemValues' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors. Here are my classes: public class Category { public int Id { get; set; } [Display

EF4.1 POCO: Why should I use ICollection

為{幸葍}努か 提交于 2019-12-06 01:11:26
问题 In nearly all examples of POCO classes created for Entity Framework 4.1 collections are defined using the ICollection interface: public class TravelTicket { public virtual int Id { get; set; } public string Destination { get; set; } public virtual ICollection<Person> Members { get; set; } } However, this causes an issue in my code where I need to access a member of the collection by index, e.g.: Person Paul = TravelTicket.Members[3]; Cannot apply indexing with [] to an expression of type

Why does my EF4.1 relationship not get set to null upon assignment of a null value?

人走茶凉 提交于 2019-12-06 00:16:03
问题 In my system I have tasks, which can optionally be assigned to contacts. So in my business logic I have the following code: if (_contactChanged) { task.Contact = contact; } If no contact was specified, the contact variable is null. This is supposed to null out the contact relationship when I submit changes, however I have noticed this isn't happening 99% of the time I do it (I have seen it happen once, but not consistently after stepping through this code over and over). When I debug, I have

Entity framework data contract

早过忘川 提交于 2019-12-05 23:33:36
I am new to WCF and Entity framework. I have a class library "A" which contains DatabaseEntities.edmx (Entity Framework objectContext ). This library is exposing a class "B" which contains a function FunctionB , internally using the entity objects. I have taken this library "A" reference into a WCF web service and inside the IService.cs - I have coded it like this: [OperationContract] void FunctionB_Proxy(); Without defining any DataContract I have gone into Service1.cs and implemented this function as below: public void FunctionB_Proxy() { ClassB x=new ClassB();//Class of ClassLibrary x