entity-framework-4.1

How can I add default values to a property when saving using Code First EF4.1?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 09:02:55
I started by creating some models like this: public abstract class EditableBase { public DateTime CreatedOn { get; set; } public DateTime ModifiedOn { get; set; } public int CreatedBy { get; set; } public int ModifiedBy { get; set; } } public class Project : EditableBase { public int ProjectId { get; set; } public string ProjectName { get; set; } } And I use this line when the app starts: Database.SetInitializer<ModelContext>( new DropCreateDatabaseIfModelChanges<ModelContext>()); A table called Projects is created with all the properties mentioned above as columns... this is exactly what I

Entity Framework Code First: FOREIGN KEY constraint may cause cycles or multiple cascade paths

佐手、 提交于 2019-11-30 08:36:08
Entity Framework Code First can generate the DB for the following POCOs. public class Item { public int Id { get; set; } public string Name { get; set; } } public class ItemPair { public int Id { get; set; } public virtual Item FirstItem { get; set; } public virtual Item SecondItem { get; set; } } I would like to establish the relationship with First and Second item via ID fields rather than the an entire "Item" class. So: public class ItemPair { public int Id { get; set; } public virtual Item FirstItem { get; set; } public int FirstItem_Id { get; set; } public virtual Item SecondItem { get;

Migration using model first approach in entity framework

浪尽此生 提交于 2019-11-30 08:35:13
I have setup a system where I have taken the model first approach as it made more logical sense for me. Now when even I have some changes in the model currently what I do is - Use the Generate database from model feature of entity framework. I create a dummy database and apply those scripts. which deletes all my data and tables first and then updates the database with the latest sql file which is generated by entity framework. Now I use the Visual Studio's schema compare feature and generate migration scripts for my local database and also for the one which is in production. I go through the

Entity Framework Code First Computed Properties

人走茶凉 提交于 2019-11-30 08:13:37
I'm using Entity Framework "Code First" approach in a ASP.NET MVC 3 web application. In my database I have several computed columns. I need to use these columns to display data (which works fine). However when I come to insert rows into this table, I'm getting the following error: The column "ChargePointText" cannot be modified because it is either a computed column or is the result of a UNION operator. Is there a way to mark the property as readonly in my class? public class Tariff { public int TariffId { get; set; } public int Seq { get; set; } public int TariffType { get; set; } public int

How do I define Foreign Key Optional Relationships in FluentAPI/Data Annotations with the Entity Framework?

笑着哭i 提交于 2019-11-30 08:09:49
I have a (sample) application with the following code: public class Posts { [Key] [Required] public int ID { get; set; } [Required] public string TypeOfPost { get; set; } public int PollID { get; set; } public virtual Poll Poll { get; set; } public int PostID { get; set; } public virtual Post Post { get; set; } } Basically, I don't know if there is a better way of doing this, but, I have a list of Posts, and, people can choose if it is a Poll or a Post , As Entity Framework doesn't work with Enums, I just store it as a string in TypeOfPost and then in the application, I programmatically query

No mapping exists from object type System.Collections.Generic.List when executing stored proc with parameters in EF 4.3

人走茶凉 提交于 2019-11-30 07:59:07
Lately I've been working on stored procedure and encountered 1 strange problem. First, I was able to successfully call a stored procedure from the database via: IList<XXXViewModel> XXXList = _context.Database.SqlQuery("spXXX").ToList(); But when I needed to pass parameters it failed: var parameters = new List<SqlParameter>(); parameters.Add(new SqlParameter("param1", param1Value)); parameters.Add(new SqlParameter("param2", param2Value)); IList<XXXViewModel> XXXList = _context.Database.SqlQuery<XXXViewModel>("spXXX @param1, @param2", parameters).ToList(); And I got the ff, error: No mapping

Can I specify a discriminator column with a table-per-type mapping?

两盒软妹~` 提交于 2019-11-30 07:55:00
问题 I have a class hierarchy that I want to map across several tables using Entity Framework 4.1 Code First. It's like table-per-type (TPT) but I also want a discrimator column. The hierarchy looks something like: public class Event { public Guid Id { get; set; } public string Code { get; set; } // discriminator public DateTime Date { get; set; } } public class Party : Event { public int AttendeeCount { get; set; } } public class BirthdayParty : Party { public int Age { get; set; } } public class

Using EF4 Code First: How can I change the Model without losing data

折月煮酒 提交于 2019-11-30 07:14:40
In my Global.asax I have the following line: Database.SetInitializer<myDbSupport> (new DropCreateDatabaseIfModelChanges<myDbSupport>()); If I don't have this, then when i change the model, the sdf database will not have the correct structure. This is ok in a dev environment, but when I move to production and want a DB structure update, i of course, can't afford to drop the table, and lose the data. Is it possible to script DB changes, and run this update before deploying the model with the changed structure? Ladislav Mrnka Initializer is for development. I consider any automatic process

Multiple DbContexts in N-Tier Application

自古美人都是妖i 提交于 2019-11-30 07:01:04
问题 I'm creating my first N-Tier MVC application and I've run into a road block with how to manage multiple DbContexts with my database first approach. I have the following layers Presentation Service (WCF) Business Data Access I don't want an entity framework reference in my service layer but I don't see how to create an Interface or something to manage two contexts. I have it working with a single context warpped in a IDatabaseFactory but I can't seem to find an approach to manage two. Below is

EF 4.1 code-first adding a trigger to a table

可紊 提交于 2019-11-30 06:38:10
What is the best way to add a trigger to a table created by code-first approach with EF 4.1? A way I'm cosidering is to execute a custom SQL query either in OnModelCreating or when Db context is being initialized. Is there a better idea? Ladislav Mrnka Using custom initializer which will execute CREATE TRIGGER SQL command is the only option if you want EF to create trigger for you ( similar code like here ). Also don't forget to include SET NOCOUNT ON at the beginning of the trigger code. But there is more complex problem related to trigger's logic. If you want trigger which will modify data