entity-framework-4.1

EF Code First Readonly column

筅森魡賤 提交于 2019-11-28 08:12:06
I am using EF Code first with database first approach. "with Database.SetInitializer(null);" My table has two columns createddate and amendddate. They are managed by SQL Server using triggers. The idea is that when data entry happens then these columns gets data via triggers. Now What I want to do is to make this read only from EF Code first point of view. I.e. I want to be able to see the createddate and ameneded dates from my app but I dont want to amend these data. I have tried using private modifiers on setter but no luck.When I try to add new data to the table it tried to enter DateTime

Deploying database changes with EF 4.1

徘徊边缘 提交于 2019-11-28 07:46:33
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 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 able to get this from source control so always correctly label / tag your production releases Database with new

EF Code First 4.1 doesn't support nvarchar(max) at all?

浪子不回头ぞ 提交于 2019-11-28 06:52:18
I've spent decent amount of time on this problem and still can't figure out why EF team makes the life so hard using Code First. So here is some sample: My POCO: The way I want the thing to look like: public class Post { public int Id {get; set;} public string Text {get; set;} } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Post>() .Property(p => p.Text) .HasColumnType("nvarchar(max)"); } The only thing that works: public class Post { public int Id {get; set;} [StringLength(4000)] public string Text {get; set;} } The problem is that when in first

How can I prevent EF “The context cannot be used while the model is being created” errors?

拜拜、爱过 提交于 2019-11-28 06:46:59
Looking at my Elmah error logs, I am seeing a few InvalidOperationException s from Entity Framework that deal with: The context cannot be used while the model is being created. This is with the latest EF CodeFirst library from Nuget. The only information I have been able to find on the net is that it is being caused by having data contexts as singletons, which is most certainly not my case. In my Windsor installer, my EF unit of work structure is being registered with: container.Register(Component.For<IUnitOfWork>() .ImplementedBy<EFUnitOfWork>() .LifeStyle .PerWebRequest); I am able to

Entity framework serialize POCO to JSON

£可爱£侵袭症+ 提交于 2019-11-28 06:46:21
问题 I'm using Ef 4.1 and I've got a POCO object I'd like to serialize to JSON, I've read there is a problem to do so when using lazy loading but I'm not sure I can because a Message can have a collection of Message . Is there any way to do this? sirialize this kind of object into JSON? My Message object looks like: public class Message { [Key] public int Id { get; set; } public int? ParentId { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime

EF - The context cannot be used while the model is being created exception during HTTP requests

一个人想着一个人 提交于 2019-11-28 06:25:21
I am receiving "The context cannot be used while the model is being created." issue in my web application in one of my webpages. This particular webpage POSTs to the server every 2-3 seconds to refresh the screen. From my testing I found that If I have 2 or more browser instances open to this page, after several minutes I receive a "The context cannot be used while the model is being created" exception from deep in the repository. This code calls a "service" to retrieve the needed data. This code is executed in an custom authorization attribute of the MVC Controller class. // Code in custom

Efficient way of checking if many-to-many relationship exists in EF4.1

拟墨画扇 提交于 2019-11-28 05:36:43
问题 I have a many-to-many relationship between two entities - Media and MediaCollection. I want to check if a certain Media already exists in a collection. I can do this as follows: mediaCollection.Media.Any(m => m.id == mediaId) However, mediaCollection.Media is an ICollection, so to me this looks like it will have to retrieve every Media in the collection from the database just to make this check. As there could be many media in a collection, this seems very inefficient. I'n thinking that I

Navigation Property not loading when only the ID of the related object is populated

自闭症网瘾萝莉.ら 提交于 2019-11-28 05:31:04
问题 I am trying to establish a many-to-one relationship. The entity that represents the “many” has a navigation property pointing back to the parent entity. It looks like this: public abstract class BaseEntity { /// <summary> /// Key Field for all entities /// </summary> /// [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } /// <summary> /// Date entity was created /// </summary> public DateTime DateCreated { get; set; } /// <summary> /// Last date Modified /

An error occurred while saving entities that do not expose foreign key properties for their relationships

青春壹個敷衍的年華 提交于 2019-11-28 05:13:43
I have a simply code in Entity Framework 4.1 code first: PasmISOContext db = new PasmISOContext(); var user = new User(); user.CreationDate = DateTime.Now; user.LastActivityDate = DateTime.Now; user.LastLoginDate = DateTime.Now; db.Users.Add(user); db.SaveChanges(); user.Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") }; db.SaveChanges(); db.Users.Add(new User() { Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") } }); db.SaveChanges(); The problem is that I get an error An error occurred while saving entities that do not expose foreign key properties

Readonly properties in EF 4.1

自古美人都是妖i 提交于 2019-11-28 05:09:07
I've faced with situation when I need to have EF readonly property in case of 'optimistic update'(you do not load current state of your domain object from database to check what properties are really changed. You just set your object as Modified and update it to database. You avoid redundant select and merge operations in this case). You can't write something like this : DataContext.Entry(entity).Property(propertyName).IsModified = false; , because setting of 'false' value is not supported and you will get an exception. (in EF 4.1) I've created a simple structure for registering readonly