Advertisement
onzulin

Aplicacion asp.net mvc5

Jul 29th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.83 KB | None | 0 0
  1. //Todo esta en la carpeta Models dentro de mi programa.
  2. //identitymodels.cs
  3. public class ApplicationUser : IdentityUser
  4.     {
  5.         //propiedades que añadire a la tabla Users de Identity
  6.         public string user_url { get; set; }
  7.         //este campo hay que manejarlo a la hora de registrar los usuarios que almacene en que fecha y hora se registraron los usuarios
  8.         public DateTime user_registered { get; set; }
  9.         public virtual ICollection<Post> Post { get; set; }
  10.         public virtual ICollection<Comment> Comment { get; set; }
  11.        
  12.  
  13.         public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
  14.         {
  15.             // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
  16.             var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
  17.             // Add custom user claims here
  18.             return userIdentity;
  19.         }
  20.     }
  21.  
  22.     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
  23.     {
  24.         public ApplicationDbContext()
  25.             : base("DefaultConnection")//, throwIfV1Schema: false)
  26.         {
  27.         }
  28.        
  29.         private string[] databases = { "SQLServer", "PostgreSQL", "MySQL" };
  30.         public string DatabaseCheck { get; set; }
  31.         public DbSet<Post> Post { get; set; }
  32.         public DbSet<Comment> Comment { get; set; }
  33.         public DbSet<Option> Option { get; set; }
  34.  
  35.         protected override void OnModelCreating(DbModelBuilder modelBuilder)
  36.         {
  37.             base.OnModelCreating(modelBuilder);
  38.             modelBuilder.Entity<IdentityUser>().ToTable("Users");
  39.             modelBuilder.Entity<ApplicationUser>().ToTable("Users");
  40.         }
  41.  
  42.         public static ApplicationDbContext Create()
  43.         {
  44.             return new ApplicationDbContext();
  45.         }
  46.  
  47.         public string CheckDb()
  48.         {
  49.             DatabaseCheck = databases[0];
  50.             return DatabaseCheck;
  51.         }
  52.  
  53.     }
  54. //dentro del fichero comment.cs
  55. namespace blogmvc.Models
  56. {
  57.     public class Comment
  58.     {
  59.         public Comment()
  60.         {
  61.             //aqui pienso hacer la discriminacion si usamos SQL Server, MySQL o PostgreSQL o SQLite que sirve para apps pequeñas
  62.             CheckDB();
  63.         }
  64.         [Key]
  65.         [Column("comment_id")]
  66.         public Int64 CommentID { get; set; }
  67.        
  68.         [Column("comment_author_email")]
  69.         public string CommentAuthorEmail { get; set; }
  70.         [Column("comment_date")]
  71.         public DateTime CommentDate { get; set; }
  72.         [Column("comment_date_gmt")]
  73.         public DateTime CommentDateGmt { get; set; }
  74.         [Column("comment_author_ip")]
  75.         public string CommentAuthorIP { get; set; }
  76.         //campo longtext en MySQL
  77.         [Column("comment_content")]
  78.         public string CommentContent { get; set; }
  79.         [Column("comment_aproved")]
  80.         public string CommentAproved { get; set; }
  81.  
  82.         /*
  83.          * Yo creo que asi deberia salir la clave externa con el nombre que yo necesito que es comment_post_id clave id de post
  84.          *
  85.          *  
  86.         */
  87.         //claves foraneas
  88.          
  89.         [Column("comment_post_id")]
  90.         public Int64 CommentPostID { get; set; }
  91.         [ForeignKey("comment_post_id")]
  92.            
  93.         public virtual Post Post { get; set; }
  94.         //clave externa de la tabla Users a la tabla Comment
  95.         //public virtual IdentityUser IdentityUser { get; set; }
  96.         //campo CommentAuthor sera el id del usuario en cuestion asociado a un mail
  97.         //[Column("comment_author")]
  98.         //public int CommentAuthor { get; set; }
  99.  
  100.         #region methods
  101.         // este metodo es practicamente igual en Post tengo que ver la forma de usar la OOP para cambiarlo
  102.         protected void CheckDB()
  103.         {
  104.             //aqui voy a preguntar por la DB y voy a cambiar segun me haga falta
  105.             ApplicationDbContext applicationDbContext = new ApplicationDbContext();
  106.             string StringDB = applicationDbContext.DatabaseCheck;
  107.             if (StringDB == "SQLServer")
  108.             {
  109.                 ColumnAttribute colum = new ColumnAttribute(CommentContent);
  110.                 colum.TypeName = "nvarchar";
  111.             }
  112.             if (StringDB == "PostgreSQL")
  113.             {
  114.                 ColumnAttribute colum = new ColumnAttribute(CommentContent);
  115.                 colum.TypeName = "text";
  116.             }
  117.             if (StringDB == "MySQL")
  118.             {
  119.                 ColumnAttribute colum = new ColumnAttribute(CommentContent);
  120.                 colum.TypeName = "LongText";
  121.             }
  122.  
  123.  
  124.         }
  125.  
  126.  
  127.     }
  128.     #endregion
  129.  
  130. }
  131.  
  132. //fichero Post.cs
  133. namespace blogmvc.Models
  134. {
  135.     public class Post
  136.     {
  137.         //por defecto pondremos el campo de PostgreSQL y asi no hay que hacer nada si el usuario usa PostgreSQL
  138.  
  139.  
  140.         //en el constructor definire el tipo de campo segun en la DB que conectemos
  141.         public Post()
  142.         {
  143.             /*veremos en que DB estamos conectados y dependiendo de eso crearemos el campo
  144.              * LongText = 4 GB MySQL
  145.              * Text campo ilimitado en PostgreSQL
  146.              * nvarchar en SQL Server            
  147.              */
  148.             //saber que DB es la que estamos manejando
  149.             CheckDB();
  150.         }
  151.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  152.         [Key]
  153.         [Column("post_id")]
  154.         public Int64 PostId { get; set; }
  155.         [Column("post_date")]
  156.         public DateTime Post_date { get; set; }
  157.         //Column System.ComponentModel.DataAnnotations.Schema.ColumnAttribute ponerlo en Typename = "text"
  158.        
  159.         // Esta columna sera definida en el metodo CheckDB
  160.         [Column("post_content")]
  161.         public string Post_Content { get; set; }
  162.  
  163.         [Column("post_title")]
  164.         public string Post_title { get; set; }
  165.         [Column("post_modified")]
  166.         public DateTime PostModified { get; set; }
  167.         [Column("post_status")]
  168.         public string PostStatus { get; set; }
  169.         [Column("post_name")]
  170.         public string PostName { get; set; }
  171.         [Column("post_type")]
  172.         public string PostType { get; set; }
  173.         [Column("post_Parent")]
  174.         public Int64 PostParent { get; set; }
  175.  
  176.         //claves foraneas
  177.         //campo que vendra de la tabla aspnetusers es la relacion con la tabla aspnetusers que tengo que crearla
  178.         //[Column("email_author")]      
  179.         //[ForeignKey("email_author")]
  180.         public virtual ApplicationUser User_ID { get; set; }
  181.         //propiedad de navegacion que hace de 1 a muchos Post de 1 y Comment de muchos
  182.         public ICollection<Comment> Comment { get; set; }
  183.  
  184.         #region methods
  185.         // este metodo es practicamente igual en Comment tengo que ver la forma de usar la OOP para cambiarlo
  186.         protected void CheckDB()
  187.         {
  188.             //aqui voy a preguntar por la DB y voy a cambiar segun me haga falta
  189.             ApplicationDbContext applicationDbContext = new ApplicationDbContext();
  190.             string StringDB = applicationDbContext.DatabaseCheck;
  191.             if (StringDB == "SQLServer")
  192.             {
  193.                 ColumnAttribute colum = new ColumnAttribute(Post_Content);
  194.                 colum.TypeName = "nvarchar";
  195.             }
  196.             if (StringDB == "PostgreSQL")
  197.             {
  198.                 ColumnAttribute colum = new ColumnAttribute(Post_Content);
  199.                 colum.TypeName = "text";
  200.             }
  201.             if (StringDB == "MySQL")
  202.             {
  203.                 ColumnAttribute colum = new ColumnAttribute(Post_Content);
  204.                 colum.TypeName = "LongText";
  205.             }
  206.  
  207.  
  208.         }
  209.        
  210.         #endregion
  211.     }
  212. }
  213. //eso son las clases que me estan dando por saco concretamente la de comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement