c# - Create db and tables with EF Code First -


i going create database , tables ef code first.

ef db

each post belongs single category , can labelled many tags. between post , category relationship many-to-one , between post , tag relationship many-to-many.

below code:

public class blogcontext : dbcontext {     public dbset<post> posts { get; set; }     public dbset<posttagmap> posttags { get; set; }     public dbset<category> categorys { get; set; }     public dbset<tag> posttags { get; set; } } public class post {     public int id { get; set; }     public string title { get; set; }     public string shortdescription { get; set; }     public string meta { get; set; }     public string urlslug { get; set; }     public bool published { get; set; }     public datetime postedon { get; set; }     public datetime? modified { get; set; }     public category category { get; set; }     public ilist<tag> tags { get; set; } } public class posttagmap {  } public class category {     public int id { get; set; }     public string name { get; set; }     public string urlslug { get; set; }     public string description { get; set; }     public ilist<post> posts { get; set; } } public class tag {     public int id { get; set; }     public string name { get; set; }     public string urlslug { get; set; }     public string description { get; set; }     public ilist<post> posts { get; set; } } 

here ef new guy's questions:

  1. do generate code posttagmap?
  2. how apply relationship between tables code?
  3. i saw people add dataannotation @ moment? because many online samples created db without step, confused.

your data model looks fine. use that. questions:

do generate code posttagmap? kind of code want generate? if mean, u define code it. dont need since have relationship defined between post , tag. can if think easier query.

how apply relationship between tables code? relationship there. such category has many posts , post belongs category.

i saw people add dataannotation @ moment? because many online samples created db without step, confused. annotaions used cross cutting concerns such validation, data type, data length , forth.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -