شروع به کار با EF Core 1.0 - قسمت 6 - تعیین نوعهای داده و ویژگیهای آنها
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۰۵/۳۱ ۱۳:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
string, int?, byte[]
int, decimal, bool, DateTime
[Required]
public string Url { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.IsRequired();
} public string FirstName { get; set; } [StringLength(450)]
public string FirstName { get; set; }
[MaxLength(450)]
public string LastName { get; set; }
[MaxLength]
public string Address { get; set; } modelBuilder.Entity<Person>() .Property(x => x.Address) .HasMaxLength(450);
modelBuilder.Entity<Person>()
.Property(x => x.Address)
.HasColumnType("nvarchar(max)"); [Column(TypeName = "varchar")]
[MaxLength]
public string Address { get; set; }
Data type 'varchar' is not supported in this form. Either specify the length explicitly in the type name, for example as 'varchar(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type.
[Column(TypeName = "varchar(max)")]
public class Person
{
public int Id { set; get; }
public DateTime? DateAdded { set; get; }
public DateTime? DateUpdated { set; get; }
[StringLength(450)]
public string FirstName { get; set; }
[MaxLength(450)]
public string LastName { get; set; }
//[Column(TypeName = "varchar")]
[MaxLength]
public string Address { get; set; }
//bit
public bool IsActive { get; set; }
//tiny Int
public byte Age { get; set; }
//small Int
public short Short { get; set; }
//int
public int Int32 { get; set; }
//Big int
public long Long { get; set; }
}
CREATE TABLE [dbo].[Persons]( [DateAdded] [datetime2](7) NULL, [DateUpdated] [datetime2](7) NULL,
[ConcurrencyCheck]
public string Name { set; get; } modelBuilder.Entity<Person>()
.Property(p => p.Name)
.IsConcurrencyToken(); [Timestamp]
public byte[] Timestamp { get; set; } modelBuilder.Entity<Blog>() .Property(p => p.Timestamp) .ValueGeneratedOnAddOrUpdate() .IsConcurrencyToken();
public byte[] RowVersion { get; set; } modelBuilder.Entity<Blog>() .Property(p => p.RowVersion) .IsRowVersion();
public virtual PropertyBuilder IsRowVersion()
{
Builder.ValueGenerated(ValueGenerated.OnAddOrUpdate, ConfigurationSource.Explicit);
Builder.IsConcurrencyToken(true, ConfigurationSource.Explicit);
return this;
} var student = new Student() {
StudentId = 50
};
using (var context = new SchoolContext()) {
context.Remove<Student>(student);
context.SaveChanges();
} In the above example, a Student with StudentId = 50 does not exist in the database. So, EF Core will throw the following DbUpdateConcurrencyException:Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded.
No type was specified for the decimal column 'Price' on entity type 'Movie'. This will cause values to be silently truncated if they do not fit in the default precision and scale.
public class Movie
{
public int ID { get; set; }
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
} protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
foreach (var property in builder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal)
|| p.ClrType == typeof(decimal?)))
{
property.SetColumnType("decimal(18, 6)");
}
}