none
Entity Framework Data Annotation x Fluence API RRS feed

  • Question

  • I would know if I can use hybrid of the Data Annotation and Fluent API.
    Once the Data Annotation I use to reflect on the interface/view and Fluent API I use to Database
    For example:

    public class Product
    {
    [Product Identification]
    public Guid ProductId { get; set; }
     
    [DisplayName(Product Name)]
    [Required(ErrorMessage= "Product Name is Required")]
    public string ProductName { get; set; }
     
    }

    public class ProductContext : DbContext
    {
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity<Product>()
    .Property(p => p.ProductId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
     
    modelBuilder.Entity<Product>()
    .Property(p => p.Name)
    .HasColumnType("varchar")
    .HasMaxLength(100);
     
      }
    }
    Friday, July 15, 2016 12:13 PM

Answers

  • Hi Welington Lourenço,

    >>I would know if I can use hybrid of the Data Annotation and Fluent API.

    Yes, you could. According to your description, I create a simple as below, it works well.

    #Models

    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    
    namespace HybirdOfDaAndFluentAPI
    {
        public class Product
        {
            public Guid ProductId { get; set; }
    
    
            [DisplayName("Product Name")]
            [Required(ErrorMessage = "Product Name is Required")]
            public string ProductName { get; set; }
    
        }
    
    }
    

    #DbContext

    namespace HybirdOfDaAndFluentAPI
    {
        using System.Data.Entity;
        using System.ComponentModel.DataAnnotations.Schema;
    
        public partial class EFDemoContext : DbContext
        {
            public EFDemoContext()
                : base("name=EFDemoContext")
            {
            }
    
            public DbSet<Product> Products { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Product>()
                            .Property(p => p.ProductId)
                            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    
                modelBuilder.Entity<Product>()
                .Property(p => p.ProductName)
                .HasColumnType("varchar")
                .HasMaxLength(100);
            }
        }
    }
    

    #Usage:

     using (var db = new EFDemoContext())
                {
                    Product p = new Product() { ProductName = "Test2" };
                    db.Products.Add(p);
                    db.SaveChanges();
                }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 18, 2016 2:43 AM
    Moderator