Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Saturday, November 09, 2013

NotMapped attribute for PetaPoco or Ignore Column while Insert/Update in Petapoco

NotMapped Property for PetaPoco or Ignore Column while Insert/Update in Petapoco

[NotMapped]   ==>  [ResultColumn]

[NotMapped] equivalent of entity framework in Petapoco is [ResultColumn]

For more information

Example:  If you have contact form model where you want user to enter captcha, but you don't want this captcha to be store in db, so In order to ignore captcha column while inserting values in database you can mark the column as [NotMapped] in entity framework and if you are using Petapoco you can use attribute [ResultColumn]

[TableName("contact")]
[ExplicitColumns]
public class contact
{
    [Column] 
    public string FullName { get; set; }
    
    [Column] 
    public string Feedback { get; set; }
    
    [ResultColumn] 
    public string Captcha { get; set; }
}

Wednesday, October 30, 2013

Entity Framework errors: An error occurred while updating the entries.

Multiple situation where Entity Framework was giving me problem while adding new record, updating record or deleting.  Then here are few things which may go wrong:


Solution 1. Check whether Database Table has Proper primary key setup.

Solution 2. Check your DB table and POCO Model class is in sync.  If you have some extra properties which you have to use in POCO class but doesn't need in DB Table, then you may create [Not Mapped] Property.
Example:

 [NotMapped]
 public bool IsEditMode { getset; }

Solution 3. Are you assigning date column a default value of DateTime.MinValue before add or update operation?  If yes, then this may also result into this problem.

Solution 4. Check if you have any decimal field in your table?  If yes then you will need to make proper configuration changes OnModelCreating.  In order to do so, add following code inside: OnModelCreating method inside your DBContext file.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Precision and scale should match sql server table decimal field.
modelBuilder.Entity().Property(x => x.YOUR_DECIMAL_FIELDNAME).HasPrecision(26, 16);
}


This error could be sometime tricky since this may occur due to many reasons, so if you run into any other situation and found the solution, Please add your comment here.

Hope this will help to narrow down your problem.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape