EF5: DB First. Generated Models and Custom Validation - entity-framework

If I use Code First Development Model, I have a full control of my code.
For Example, I have model: User.
public class User {
[Key]
public int id { get; set; }
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Login can't be empty")]
public string Login { get; set; }
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Name can't be empty")]
public string FirstName { get; set; }
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Last name can't be empty")]
public string LastName { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public string Phone { get; set; }
[Required]
public DateTime CreatedAt { get; set; }
[Required]
public DateTime LastLogin { get; set; }
public List<Post> PostList { get; set; }
}
I have ability to create my own validation, like at model "User".
In case, if I use DB First Development Model, I need to use ADO.NET Entity Data Model to generate models. I have 3 tables:
I have generated files:
DBContext.edmx
- DBContext.Context.tt
- DBContext.Designer.cs
- DBContext.edmx.diagram
- DBContext.tt
- Comment.cs
- DBContext.cs
- Post.cs
- Comment.cs
and code below:
public partial class DBContext : DbContext
{
public DBContext()
: base("name=DBContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Comment> Comment { get; set; }
public DbSet<Post> Post { get; set; }
public DbSet<User> User { get; set; }
}
public partial class User
{
public User()
{
this.Comment = new HashSet<Comment>();
this.Comment1 = new HashSet<Comment>();
this.Post = new HashSet<Post>();
this.Post1 = new HashSet<Post>();
}
public int id { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public System.DateTime CreatedAt { get; set; }
public System.DateTime LastLogin { get; set; }
public virtual ICollection<Comment> Comment { get; set; }
public virtual ICollection<Comment> Comment1 { get; set; }
public virtual ICollection<Post> Post { get; set; }
public virtual ICollection<Post> Post1 { get; set; }
public virtual User User1 { get; set; }
public virtual User User2 { get; set; }
}
public partial class Post
{
public Post()
{
this.Comment = new HashSet<Comment>();
}
public int id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string TextFormatted { get; set; }
public System.DateTime CreatedAt { get; set; }
public System.DateTime UpdatedAt { get; set; }
public Nullable<int> CreatedById { get; set; }
public Nullable<int> UpdatedById { get; set; }
public Nullable<int> UserId { get; set; }
public virtual User User { get; set; }
public virtual User User1 { get; set; }
public virtual ICollection<Comment> Comment { get; set; }
}
public partial class Comment
{
public int id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string TextFormatted { get; set; }
public System.DateTime CreatedAt { get; set; }
public System.DateTime UpdatedAt { get; set; }
public Nullable<int> CreatedById { get; set; }
public Nullable<int> UpdatedById { get; set; }
public Nullable<int> PostId { get; set; }
public virtual User User { get; set; }
public virtual User User1 { get; set; }
public virtual Post Post { get; set; }
public virtual Comment Comment1 { get; set; }
public virtual Comment Comment2 { get; set; }
}
Questions:
1. As I understand, if I use DB First Development Model, I can't use my own models for data access, just models/classes, generated by ADO.NET Entity Data Model?
I tried to use my own model "UserOwn" except generated "User", so I got an error "Unable to retrieve metadata for 'TestDBFirst02.Models.UserOwn'". Expected.
2. Can I use both Development Model: Code First and DB First inside one project?
3. If I need to use generated models, what I need to do, when I want to use my own validation? I tried to modificate generated model, and it works:
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Login can't be empty")]
public string Login { get; set; }
But, if I need to update model from DB, of course my validation code overwrites by ADO.NET Entity Data Model and attributes disappear. How can I overcome the situation with my own validation?

Answer the 3-rd question: need to use [MetadataTypeAttribute(typeof(UserValidation)] attribute and create validation class, so code is:
[MetadataType(typeof(UserValidation))]
public partial class User {}
public class UserValidation
{
[StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")]
[Required(ErrorMessage = "* Login can't be empty")]
public string Login { get; set; }
}
with the same! namespace, as User model has.
Read more:
http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/enhancing-data-validation
http://jnye.co/Posts/19/adding-validation-to-models-created-by-entity-framework-database-first-c
http://weblogs.asp.net/scottgu/asp-net-mvc-2-model-validation
Database First Validation
Adding validation to model with Database First model (EF 5)
http://www.elevenwinds.com/data-validation-in-asp-net-mvc-database-first

Related

Adding an entity in EF Core

I am using EF Core db first approach, .NET6. I have the following classes:
public class Doctor
{
[Key]
public int DoctorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Specialization { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Designation { get; set; }
public bool IsDeleted { get; set; }
}
and
public class Patient
{
[Key]
public int PatientId { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string NIC { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public DateTime DOB { get; set; }
public string Gender { get; set; }
public string Reference { get; set; }
public short SerialNumberYear { get; set; }
public int SerialNumber { get; set; }
[Required]
public int DoctorId { get; set; } //need to assign Primary doc. Is this correct?
public Doctor Doctor { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public string CreatedBy { get; set; }
public List<Doctor> SecondaryDoctors { get; set; }//need to assign a list of secondary docs. is this correct?
public bool SMS_Allowed { get; set; }
public bool Email_Allowed { get; set; }
public string SpecialConcern1 { get; set; }
public string SpecialConcern2 { get; set; }
public string SpecialConcern3 { get; set; }
public string SpecialConcern4 { get; set; }
}
The Patient class needs to have a Primary doctor assigned and a list of Secondary doctors assigned. What entries should I make in the Patient class to accomodate this requirement? I tried the entries shown above with comments. Is that correct? When I add a patient with this code, I get the following error when creating a new patient record:
Duplicate entry '1' for key 'doctors.PRIMARY'
So when creating a patient, why is efcore trying to create a doctor record?

EF6 generates all the entity classes (tt .cs) in single file (eg. model.cs) and not as separate .cs file

I am using EF6. I have 2 tables Events and Users for which I used database first approach to generate the EDMX models and entity classes. Everything works fine but the entity classes are coming under a single file in this case EventSample.cs(shown below). I am not getting separate files for each entity as Event.cs and User.cs. Please let me know if this is correct and if not how to rectify it?
public partial class Event
{
public Event()
{
this.Users = new HashSet<User>();
}
public int Id { get; set; }
public string Title { get; set; }
public System.DateTime Time { get; set; }
public string Location { get; set; }
public string Description { get; set; }
public int Creator_Id { get; set; }
public virtual User User { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public partial class User
{
public User()
{
this.Events = new HashSet<Event>();
this.Events1 = new HashSet<Event>();
}
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public virtual ICollection<Event> Events { get; set; }
public virtual ICollection<Event> Events1 { get; set; }
}

One to many relation with cascade giving error

I am learning EF Code First with migrations, I have 3 entities :
[User] 1--->* [Call] 1--->* [ETA]
Code :
User.cs
public class User
{
[Key]
public int Id { get; set; }
public Guid LongId { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Call> Calls { get; set; } // many calls
public User()
{
LongId = Guid.NewGuid();
}
}
Call.cs
public class Call
{
[Key]
public int Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string BreakdownNo { get; private set; }
[Required,MaxLength(32)]
public string Customer { get; set; }
[Required,MaxLength(32)]
public string TrailerNo { get; set; }
[Required, MaxLength(32)]
public string DepotContact { get; set; }
[Required, MaxLength(48), RegularExpression(#"^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$")]
public string DepotEmail { get; set; }
[Required, MinLength(9), MaxLength(32)]
public string DepotPhone { get; set; }
[Required, MaxLength(32)]
public string DriverContact { get; set; }
[Required, MinLength(9), MaxLength(32), RegularExpression(#"^(7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$")]
public string DriverPhone { get; set; }
[Required, MaxLength(256)]
public string LocatedAtFreeText { get; set; }
[Required, MaxLength(8), RegularExpression(#"^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,1}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$")]
public string LocatedAtPostCode { get; set; }
[Required, MaxLength(16)]
public string StartupNo { get; set; }
[Required]
public bool IsLoaded { get; set; }
[Required, MaxLength(256)]
public string FaultDescription { get; set; }
[Required]
public DateTime StartTime { get; set; }
public DateTime? EndTime { get; set; }
public string Status { get; set; }
public virtual User Controller { get; set; } // 1 controller
public virtual ICollection<ETA> ETAs { get; set; } // many ETAs
public Call()
{
StartTime = DateTime.Now;
ETAs = new List<ETA> { new ETA() };
Status = "Logged";
}
}
ETA.c
public class ETA
{
[Key]
public int Id { get; set; }
[Required]
public TimeSpan Value { get; set; }
public int CallId { get; set; }
public ETA()
{
Value = TimeSpan.FromMinutes(90);
}
}
I would like it so when I delete the User it deletes all of the Calls for the User, which in turn deletes all of the ETAs for those Calls.
When I delete a User row from the Database (using database explorer) it gives me an error :
No rows were deleted.
A problem occurred attempting to delete row 201.
Error Source: .Net SqlClient Data Provider.
Error Message: The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Calls_dbo.Users_Controller_Id". The conflict occurred in database "BreakdownDb", table "dbo.Calls", column 'Controller_Id'.
You can turn on the Cascade Delete option in Entity Framework, here you will find more info:
http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx
The solution was to add OnModelCreating method to my DbContext class :
public class BreakdownDb : DbContext
{
public DbSet<Call> Calls { get; set; }
public DbSet<User> Users { get; set; }
public BreakdownDb(): base("name=DefaultConnection") {}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasMany(x => x.Calls).WithRequired();
modelBuilder.Entity<Call>().HasMany(x => x.ETAs).WithRequired();
}
}

The column name is not valid

I get a common error that probably is easy to solve but I have no clue how. This is the message I get.
The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ]
This is my classes representing my entities:
public class BlogContext : DbContext
{
public BlogContext()
: base("SqlCeServices")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
[Table("aspnet_Users")]
public class User
{
[Required]
public Guid ApplicationId { get; set; }
[Required]
public Guid UserId { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string LoweredUserName { get; set; }
public string MobileAlias { get; set; }
[Required]
public bool IsAnonymous { get; set; }
[Required]
public DateTime LastActivityDate { get; set; }
}
public class Blog
{
[Key]
public int BlogId { get; set; }
[Required]
public string Name { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
public virtual ICollection<User> Editors { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
[Key]
public int PostId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public string Abstract { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateLastEdited { get; set; }
public virtual User UserId { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual Blog BlogId { get; set; }
}
public class Category
{
[Key]
public int CategoryId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public string Description { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual Blog BlogId { get; set; }
}
public class Tag
{
[Key]
public int TagId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public virtual Blog BlogId { get; set; }
}
Consider changing your Post class so that BlogId references the FK to the associated Blog instead of the Blog object itself. Add a new property Blog to reference the actual Blog object.
public class Post
{
[Key]
public int PostId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public string Abstract { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateLastEdited { get; set; }
public virtual User UserId { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
You can try...ForeignKey annotation
public virtual int BlogId { get; set; }
[ForeignKey("BlogId ")]
public virtual Blog Blog { get; set; }
Detach the connection from DB, Delete any App Data related to application and rebuild and try again, It worked for me.

Entity Framework 4.3 CF many - to - many relationship saving object?

When creating many to many relationship using EF 4.3 code first approach, I cannot save data to connecting table, also cannot any examples on how to fill this table using saving object to Icollection... Here is my example:
MODELS
public class Hospital
{
//PK
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string County { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Guid User_Id { get; set; }
//FK
public virtual ICollection<Operator> Operators { get; set; }
}
public class Operator
{
//PK
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
public string Email { get; set; }
//FK
public virtual ICollection<Hospital> Hospitals { get; set; }
}
public class Project: DbContext
{
public DbSet<Hospital> Hospitals { get; set; }
public DbSet<Operator> Operators { get; set; }
}
CONTROLLER
public void AddOperater()
{
Hospital h = new Hospital();
h = db.Hospitals.Single(a=>a.Id ==1);
var o = new Operator();
o.FirstName = "John";
o.LastName = "Doe";
o.Dob = new DateTime(1988,2,12);
o.Email = "johndoe#gmail.com";
o.Hospitals.Add(h);
db.SaveChanges();
}
With this approach I keep getting error here: o.Hospitals.Add(h); even when my Hospital instance is filled with data. How exactly to save data to both tables, the dbo.Operators and dbo.OperatorHospital which is relationship table?
o.Hospitals.Add(h) will fail because the list is a null list. You cannot call Add() on a null list. Typically most people get around this by instantiating the list in the constructor of the entity... like so... the current line is blowing up due to a CSharp issue.
public class Hospital
{
//PK
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string County { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Guid User_Id { get; set; }
//FK
public virtual ICollection<Operator> Operators { get; set; }
public Hospital()
{
Operators = new List<Operator>();
}
}
public class Operator
{
//PK
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
public string Email { get; set; }
//FK
public virtual ICollection<Hospital> Hospitals { get; set; }
public Operator()
{
Hospitals = new List<Hospital>();
}
}