我想用我的模型类初始化新的GUID。我无法初始化GUID,上次登录IP地址。
Initializer不会在数据库上创建Kullanici表的行。为什么?我不明白。
如何使用GUID,IP地址,DateTime.Now
等添加新行?你可以帮帮我吗?
我正在使用Entity Framework 6,MVC 5,SQL Server 2012和ASP.NET 4。
Kullanici.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace agiEmlak.Entities
{
public class Kullanici
{
//public Kullanici()
//{
// GID = Guid.NewGuid();
//}
[Key]
public int ID { get; set; }
public Guid GID { get; set; }
public string KullaniciAdi { get; set; }
public string Ad { get; set; }
public string Soyad { get; set; }
public string Sifre { get; set; }
public string Eposta { get; set; }
public DateTime KayitTarihi { get; set; }
public string SonIPAdres { get; set; }
public DateTime SonLoginTarihi { get; set; }
public string AktivasyonKodu { get; set; }
public string CepTelefonu { get; set; }
public string IsTelefonu { get; set; }
public bool? Aktif { get; set; }
public bool? Silinmis { get; set; }
public string AdSoyad
{
get
{
return Ad + " " + Soyad;
}
}
public virtual ICollection<Emlak> Emlaks { get; set; }
}
}
ilanInitializer.cs :
using agiEmlak.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
namespace agiEmlak.Dal.Concrete.EntityFramework
{
public class ilanInitializer : DropCreateDatabaseIfModelChanges<ilanDBContext>
{
protected override void Seed(ilanDBContext context)
{
var kullanicilar = new List<Kullanici>
{
new Kullanici{ GID=Guid.NewGuid(), KullaniciAdi="halilkoca", Ad="Halil", Soyad="Koca", Eposta="halilkoca@outlook.com", Sifre="12551255", CepTelefonu="05415013031", IsTelefonu="02526133428", KayitTarihi=DateTime.Parse("25/10/2015"), AktivasyonKodu="5", SonIPAdres=Dns.GetHostName().ToString(), SonLoginTarihi= DateTime.Now},
new Kullanici{ GID=Guid.NewGuid(), KullaniciAdi="halil", Ad="Halil", Soyad="Koca", Eposta="halilkoca.ilan@gmail.com", Sifre="12551255", CepTelefonu="05415013031", IsTelefonu="02526133428", KayitTarihi=DateTime.Parse("26/10/2015"), AktivasyonKodu="10", SonIPAdres=Dns.GetHostName().ToString(), SonLoginTarihi= DateTime.Now}
};
kullanicilar.ForEach(s => context.Kullanicis.Add(s));
context.SaveChanges();
}
}
}
ilanDbContext.cs :
using System.Data.Entity;
using agiEmlak.Entities;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace agiEmlak.Dal.Concrete.EntityFramework
{
public class ilanDBContext:DbContext
{
public ilanDBContext(): base("ilanDBContext")
{
}
public DbSet<Adres> Adress { get; set; }
public DbSet<Ayar> Ayars { get; set; }
public DbSet<Detay> Detays { get; set; }
public DbSet<DetayTaksonomi> DetayTaksonomis { get; set; }
public DbSet<Emlak> Emlaks { get; set; }
public DbSet<EmlakKonutTuruMapping> EmlakKonutTuruMappings { get; set; }
public DbSet<EmlakTipi> EmlakTipis { get; set; }
public DbSet<EmlakTuru> EmlakTurus { get; set; }
public DbSet<Il> Ils { get; set; }
public DbSet<Ilce> Ilces { get; set; }
public DbSet<KonutTipi> KonutTipis { get; set; }
public DbSet<Kullanici> Kullanicis { get; set; }
public DbSet<Mahalle> Mahalles { get; set; }
public DbSet<Ozellik> Ozelliks { get; set; }
public DbSet<OzellikMapping> OzellikMappings { get; set; }
public DbSet<OzellikTaksonomi> OzellikTaksonomis { get; set; }
public DbSet<Ulke> Ulkes { get; set; }
/// <summary>
/// Database üzerine Tablo isimleri tekil olarak kaydedilir.
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
解决方案:
将Seed
方法执行添加到Global.asax.cs
:
Database.SetInitializer<ilanDBContext>(new ilanInitializer());
感谢。