entity-framework-4.1

EF 4.1 code first causes weird (login) runtime errors

淺唱寂寞╮ 提交于 2019-12-04 11:31:00
问题 I'm using EF 4.1 code first. Running into a very weird situation: Database does not exist, code is executed and as soon as the code wants to execute a query against the repository (using also repository pattern) MyRepsitory.Get(whereClause) i'm getting this error: Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'sa'. OK! So i tried to find what it is caused by and did the following: set a break point at the line which caused the error and as

Entity Framework 4.1 Code First - Should many relationship ICollections be initialised

爱⌒轻易说出口 提交于 2019-12-04 11:27:40
In Entity Framework 4.1 when creating a POCO, should the class be coded to initialise the Many relationships or is there some reason to allow the Entity Framework to have control over these properties? public class Portfolio { private ICollection<Visit> _visits; public virtual ICollection<Visit> Visits { get { if (_visits == null) { _visits = new List<Visit>(); } return _visits; } set { _visits = value; } } } Or public class Portfolio { public virtual ICollection<Visit> Visits { get; set; } } Is there a better pattern still? The first version is correct. It will allow you to initialize

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

浪尽此生 提交于 2019-12-04 09:35:17
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 model. Here's how the table looks: [SYSTEMINVOICES] INVOICE_ID int INV_TOTAL decimal INVOICE_SEQ

How to update only one table for model from database with Entity Framework?

大憨熊 提交于 2019-12-04 08:03:21
问题 I have a model generated from db with Entity Framework. When I have any change in database, I update model from database to get the change in model. But this update is applied to all entities (tables) included in model. Now I add a new column in a table Tab1. I don't want to update model from database as some other changes that I don't want to include in model. I can add the new property in model for entity Tab1 manually. then it caused mapping error. So I need to update Model.Store for the

Entity Framework 4.1: Override IEnumerable<ValidationResult> Validate

泄露秘密 提交于 2019-12-04 07:46:36
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 call base validate */ if (this.Name == "dog") { yield return new ValidationResult("Invalid Name From

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

拟墨画扇 提交于 2019-12-04 07:22:16
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 key to a Unique Constraint/Index instead of to the Primary Key. How can I map this properly in Entity

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

我们两清 提交于 2019-12-04 06:41:23
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 specify a category, which means a dropdown list of categories. I started off by just returning an

How to perform CRUD with Entity Framework Code-First?

*爱你&永不变心* 提交于 2019-12-04 05:45:36
I am having a really hard time updating and deleting many to many relationships with EF Code-first. I have a fairly simple Model: public class Issue { [Key] public int IssueId { get; set; } public int Number { get; set; } public string Title { get; set; } public DateTime Date { get; set; } public virtual ICollection<Creator> Creators { get; set; } } public class Creator { [Key] public int CreatorId { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public virtual ICollection<Issue> Issues { get; set; } } public class

Cannot apply indexing with [] to an expression of type ICollection

大兔子大兔子 提交于 2019-12-04 04:27:12
问题 Can someone please provide code to fix this error? "Cannot apply indexing with [] to an expression of type 'ICollection' Essentially, I'm trying to save/bind a value from a collection of objects. @model MVC3.Models.Parent @Html.EditorFor(model => model.Bs[0].Val) public class A { public int Name { get; set; } public virtual ICollection<B> Bs { get; set; } } public class B { public int Val { get; set; } public virtual A A { get; set; } } 回答1: ICollection s are not ordered, so that cannot be

Entity Framework DateTime column - GetDate() value Insertion

限于喜欢 提交于 2019-12-04 04:10:20
I have a table with DateCreated and DateUpdated columns and using Entity Framework to insert/update values to the database. I need the DateCreated column to get the SQL Server's GetDate() value on insertion only. DateUpdated column value should always get updated with current GetDate() value on insertion and update both. For the DateCreated column, I've set StoreGeneratedPattern="Computed" and on SQL Server table I've set default value of the column to be GetDate(), which works nicely as expected. For the DateUpdated column I could not find a way to get the GetDate() value automatically set