2

I am using asp.net mvc and EF for its DB. I use the "generate from database" tool. Everything is ok but the validation. I want the validations match the check constraints I have added to db before.

This is my db table creator query :

use MagicContact

create table Contacts
(
    ID int primary key not null identity,
    name nvarchar(50),
    last_name nvarchar(50),
    mobile nvarchar(11),
    country int foreign key references Country(ID),
    constraint CX_Contacts_mobile check(mobile like '[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or mobile like '0[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
)

The generated model is as below and it hasn't my constraints :

namespace MVCMajicContacts
{
    using System;
    using System.Collections.Generic;

    public partial class Contact
    {
        public int ID { get; set; }
        public string name { get; set; }
        public string last_name { get; set; }
        public string mobile { get; set; }
        public int country { get; set; }

        public virtual Country Country1 { get; set; }
    }
}

And I couldn't find any good way.

halfer
  • 19,824
  • 17
  • 99
  • 186
ConductedClever
  • 4,175
  • 2
  • 35
  • 69
  • 1
    I explain more : i want the constraints (specially check constraints with like operators) to be auto generated as the columns itself. is there any way? – ConductedClever Aug 03 '14 at 09:59

2 Answers2

0

I think this is good enough. string uses nvarchar(max).From the storage prospective there are no differences between nvarchar(max) and nvarchar(N) when N < 4000.

For country you have lazy loading, which you can make it eager to load everything at once. but that is a choice. If you consider performance, make it eager.

Other than that, it should work and it is fine.

Take a look at : Are there any disadvantages to always using nvarchar(MAX)?

Community
  • 1
  • 1
DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • I know the nvarchar uses the required space but the value in '()' is the limit and i want to have constraints. – ConductedClever Aug 03 '14 at 09:54
  • If you want to use constraint for validation, you should do it in your app anyway. You can use annotations on your model for that purpose. `[MinLength]`, `[MaxLength]` for example. – DarthVader Aug 03 '14 at 13:12
  • @DarthVader is right.You can use DataAnnotations and create ViewModels for your views which is recommended and Good practice. – Husrat Mehmood Aug 03 '14 at 13:40
-1

To include constraints in your model you can use

using System.ComponentModel.DataAnnotations;

Entity Frameworks applies those constraints as it did as mensioned by DarthVader.

you can specify constraints for every model property as follows for model first. Don't match model classes directly with the Database its not best practice.Best is to go for ViewModels and in view models you can use Constraints as follows by using DataAnnotations class. below is an example

public partial class Contact
    {


        [Required]
        [Display(Name="Name")]
        [StringLength(100)]
        public string name { get; set; }  



    }
ConductedClever
  • 4,175
  • 2
  • 35
  • 69
Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22
  • the problem is about my constraint. how to virtualize the like operand in the check constraint ? – ConductedClever Aug 03 '14 at 09:57
  • Possible via data annotations as i have mensioned above and have given you an example as well.Database constraints are not loaded to model classes however these are applied when you will insert data into your database but if you want your model to validate your data input you can use ViewModels and can use Data Annotations.which is proper procedure. – Husrat Mehmood Aug 03 '14 at 13:39
  • I have done these types of things by regular expressions ... thanks @Husrat Mehmood – ConductedClever Aug 03 '14 at 19:40