0

I am migrating to SQL Server from MySql and re-writing a website in C# (I was/am a vb.net guy) using code-first.

I wrote the following class

namespace DomainClasses.GeographyDb
{
  public class PostalCode
  {
    [Key]
    public int PostalCodeId { get; set; }

    [Required]
    [DataType(DataType.PostalCode)]
    public string PostCode { get; set; }

    [Required, ForeignKey("City")]
    public int CityId { get; set; } 

    [Required, ForeignKey("TimeZone")]
    public int TimeZoneId { get; set; }

    [Required, MaxLength(30)]
    public string AreaRegionPhonePrefixCode { get; set; }

    [MaxLength(10)]
    public string TaxRegionCode { get; set; }

    public virtual City City { get; set; }

    public virtual TimeZone TimeZone { get; set; }
}

I wanted to see what Entity Framework would write if it were creating the class so I created a test project and using code first from database option I imported an existing database of exact same fields as I wrote the custom classes for.

Here is what it produced;

namespace CodeFirstCreationTest
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class postalcode
    {
        public long PostalCodeId { get; set; }

        [Required]
        [StringLength(10)]
        public string PostCode { get; set; }

        public long CityId { get; set; }

        public long TimeZoneId { get; set; }

        [Required]
        [StringLength(10)]
        public string AreaRegionPhonePrefixCode { get; set; }

        [StringLength(10)]
        public string TaxRegionCode { get; set; }

        public virtual city city { get; set; }

        public virtual timezone timezone { get; set; }
    }
}

I have a number of questions as to the differences;

  1. Why did Entity Framework put all the using statements INSIDE the namespace declaration? I thought they were always to be outside and before the code

  2. StringLength is a data validation annotation. Why did Entity Framework put that attribute on the property, isn't it for UI validation? And now that I think of it I always put Required attribute on classes, is that a data validation annotation as well and not a database annotation?

  3. It created a database class using fluent API and modelbuilder. Is it better to annotate the class properties or do it using the modelBuilder in the OnModelCreating method?

  4. Why did Entity Framework make this class partial and apparently all the classes it generates as partial?

dinotom
  • 4,990
  • 16
  • 71
  • 139

1 Answers1

0
  1. Using statements are sometimes generated inside the actual namespace declaration. Usually there is no difference. Only in some cases is it a bit different. Please see this answer for more info, since it is explained better than I would be able to: https://stackoverflow.com/a/151560/1757695
  2. StringLength and Required are both database annotations and the according operations are executed right before saving the data in the database. For example if you were trying to save a User and the user had a UserName property decorated with Required data annotation, you would get an error only when you called SaveChanges method and not when you assigned the value to the property.
  3. I believe it is personal preference. I prefer data annotations and as little as possible fluent API. There are still some things that you can't do with data annotations but can do with fluent API.
Community
  • 1
  • 1
Phoenix
  • 913
  • 1
  • 8
  • 18
  • Your answer to #2 doesnt jive to what i found after posting – dinotom Dec 27 '14 at 15:16
  • MaxLength is used for the Entity Framework to decide how large to make a string value field when it creates the database. From MSDN: Specifies the maximum length of array or string data allowed in a property. StringLength is a data annotation that will be used for validation of user input. From MSDN: Specifies the minimum and maximum length of characters that are allowed in a data field. – dinotom Dec 27 '14 at 15:16
  • Well, I think you asked if Required and StringLength are database annotations or data validation annotations. Maybe I have misunderstood. – Phoenix Dec 27 '14 at 21:43
  • yes, and StringLength is clearly a validation annotation from the MSDN definition, see my comment above, but thanks for replying. – dinotom Dec 28 '14 at 00:41