entity-framework-4.1

TransactionScope, where is begin transaction on sql profiler?

一曲冷凌霜 提交于 2019-12-30 05:57:14
问题 i need to do something like this on a transaction context using(var context = new Ctx()) { using (TransactionScope tran = new TransactionScope()) { decimal debit = 10M; int id = 1; var data = context.Cashier .Where(w => w.ID == id) .Select(s => new{ s.Money }) .Single(); Cashier cashier = new Cashier(){ ID = id }; context.Cashier.Attach(cashier); cashier.Money = data.Money - debit; context.Entry(cashier).Property(p => p.Money ).IsModified = true; context.SaveChanges(SaveOptions.None); tran

What is the best way to manually generate Primary Keys in Entity Framework 4.1 Code First

浪子不回头ぞ 提交于 2019-12-30 03:18:11
问题 What is the best way to manually generate Primary Keys in Entity Framework 4.1 Code First? I am programming ASP.NET MVC 3 and I use a repository pattern. I currently generate keys in a sequential order by using the code below: 'Code First Class Public Class Foo <Key()> <DatabaseGenerated(DatabaseGeneratedOption.None)> Public Property iId As Integer Public Property sBar As String End Class 'Context Class Public Class FooBarContext : Inherits DbContext Public Property Foos As DbSet(Of Foo) End

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

与世无争的帅哥 提交于 2019-12-30 03:14:09
问题 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

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

不问归期 提交于 2019-12-30 02:38:27
问题 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

Wrapping DbSet<TEntity> with a custom DbSet/IDbSet?

隐身守侯 提交于 2019-12-29 14:21:31
问题 First off, I think this is somewhat ridiculous to do but the other members of my team insist upon it and I can't come up with a good argument against it other than "I think it's dumb"... What we're trying to do is create a completely abstract data layer and then have various implementations of that data layer. Simple enough, right? Enter Entity Framework 4.1... Our end goal here is that the programmers (I do my best to stay only on the data layer) never want to have to be exposed to the

entity framework - many to many relationship

依然范特西╮ 提交于 2019-12-29 07:58:09
问题 Hi I try use Many to Many relationship with EF Fluent API. I have 2 POCO classes. public class Project { public int ProjectId { get; set; } public virtual ICollection<Author> Authors { get; set; } public Project() { Authors = new List<Author>(); } } public class Author { public int AuthorId { get; set; } public virtual ICollection<Project> Projects { get; set; } public Author() { Projects = new List<Project>(); } } And I map many to many relationship with this part of code: ////MANY TO MANY

Entity Framework | Code First | Mapping of sub property from CultureInfo.Name

ε祈祈猫儿з 提交于 2019-12-29 06:35:10
问题 I've got an entity like this : public class Course { public CultureInfo Culture {get; set;} : } And I want to map just the Name property of CultureInfo to a single column in the table that Entity Framework generates for me. How would I go about this? Currently I've tried looking at the OnModelCreating() method of the DbContext, but I'm not finding anything that is standing out. I'm using Entity Framework 4.1 in and MVC 3 application with a Code First approach. Many thanks. 回答1: Unfrotunatelly

Triggering EF migration at application startup by code

こ雲淡風輕ζ 提交于 2019-12-28 18:07:08
问题 Using Entity Framework Migrations (Beta1), using Update-Database command is all good during development. But when the application is running on some customer's server somewhere, I really want my application to automatically update it's database schema to the latest version when it's started. Is this possible? Documentation is scarce. 回答1: They aren't providing a way to do this until RTM, at which point they have promised a command line app and a msdeploy provider. Source: http://blogs.msdn

Cascade Delete Rule in EF 4.1 Code First when using Shared Primary Key Association

旧街凉风 提交于 2019-12-28 16:02:48
问题 I implemented a bidirectional 1:1 relationship based on this answer: Primary /Foreign Key in Entity Framework I define the bidirectional relation this way: public class Student { public virtual int StudentId { get; set; } public virtual Anamnesis Anamnesis { get; set; } . . . } public class Anamnesis { [Key, ForeignKey("Student")] public int AnamnesisId { get; set; } public virtual Student Student { get; set; } . . . } where, Student is the principal entity and Anamnesis it the entity that

Deploying database changes with EF 4.1

只谈情不闲聊 提交于 2019-12-28 12:33:10
问题 Does anyone have any best practices around deploying database changes in an EF 4.1 code-first solution? I know MS does not currently support database migrations for EF 4.1, but obviously people are going to need to do this from time to time. Thanks 回答1: Once you deployed database to production you must do incremental changes. It means that before you deploy next version you must prepare two databases in your dev box: Database with DB schema currently deployed in production - you should be