C#.NET 4.0的MVC插入与主键的记录 [英] C# .NET 4.0 MVC Inserting a record with primary key

查看:249
本文介绍了C#.NET 4.0的MVC插入与主键的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个表的数据库称为站点
这个表有列,SITEID,名称,标签,说明,URI,与SITEID作为主键(它没有被设置为,因为我们的身份希望)



我们已经使用.NET 4.0 MVC与C#被添加我们自己的ID和已经设置的一切行动中,我们需要的代码。
,我们可以从数据库中选择的东西,并显示他们,所以我们知道这是工作。
但是,当我们尝试将我们得到了一个无法插入NULL值插入列'SITEID错误。



如果我设置列作为一个标识,使其自动生成,或者如果我脱掉的主键,然后它是好的,但正如我说,这应该是一个主键,我们要插入了自己的ID的。



我的代码如下(我们得到错误的的SaveChanges(),但在调试器已经确认并SITEID绝对是被分配一个int)



网站

 公共类网站
{
[关键]
公众诠释SITEID {搞定;组; }
公共字符串名称{;组; }
公共字符串标签{搞定;组; }
公共字符串描述{搞定;组; }
公共字符串URI {搞定;组; }
}



CMSModels



 公共类CMSModels:的DbContext 
{
//公共DbSet< ContentTypeModel> {的ContentType获取;组; }
//公共DbSet< LayoutModel>布局{搞定;组; }
//公共DbSet< PageModel>第{搞定;组; }
//公共DbSet< PageZoneModel> PageZone {搞定;组; }
公共DbSet<网站与GT;网站{搞定;组; }
//公共DbSet< ZoneContentModel> ZoneContent {搞定;组; }
//公共DbSet< ZoneTypeModel> ZoneType {搞定;组; }
}



HomeController的:



 私人CMSModels模式=新CMSModels(); 

公众的ActionResult指数()
{
位的网站=新的站点{SITEID = 4,名称=名称,说明=说明,标签=,URI =};

models.Site.Add(网站);
models.SaveChanges();

返回查看(models.Site.ToList());
}



我不明白为什么我收到这个错误,所以任何想法可以理解的。



如果您需要查看任何其他代码,请让我知道。



修改:



只是为了延长这个问题了一下,似乎这只是发生在主键这是int类型的



如果我们把数据库列的数据类型为nvarchar,并在代码中的字符串数据类型,它工作正常。



这是刚在实体框架4中的错误,还是我们只是做错了什么?



如果我们不能找到一个方法来插入到整数我们可能只是有一个主键列使用字符串,而不是和验证它们,以确保它们都是数字。


解决方案

如果你想要一个没有身份的主键列,因此你手动设置ID,你有2个可能的解决方案。



1。具有以下属性(DatabaseGenerated ...)装饰您的ID propertie。



 公共类网站
{
〔要求,重点,DatabaseGenerated(DatabaseGeneratedOption.None)
公众诠释SITEID {搞定;组; }
公共字符串名称{;组; }
公共字符串标签{搞定;组; }
公共字符串描述{搞定;组; }
公共字符串URI {搞定;组; }
}



2。不要在你的的DbContext



 公共类CMSModels使用模型构建器:的DbContext 
{
//公共DbSet< ContentTypeModel> {的ContentType获取;组; }
//公共DbSet< LayoutModel>布局{搞定;组; }
//公共DbSet< PageModel>第{搞定;组; }
//公共DbSet< PageZoneModel> PageZone {搞定;组; }
公共DbSet<网站与GT;网站{搞定;组; }
//公共DbSet< ZoneContentModel> ZoneContent {搞定;组; }
//公共DbSet< ZoneTypeModel> ZoneType {搞定;组; }

保护覆盖无效OnModelCreating(模型构建器模型构建器)
{
base.OnModelCreating(模型构建器);
modelBuilder.Entity<网站与GT;()房产(R = GT; r.SiteID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}


We have a database with a tables called 'Sites' This table has the columns, SiteID, Name, Tags, Description, URI, with SiteID being a primary key (It is not set as an Identity because we want to add our own ID)

We have been using .NET 4.0 MVC with C# and have setup everything up in the code which we need. We can select things from the database and display them so we know that is working. But when we try to insert we get a Cannot insert the value NULL into column 'SiteID' error.

If I set the column as an Identity so that it auto generates, or if I take off the primary key then it is fine, but as I said it should be a primary key and we want to insert out own ID's.

My code is below (We get the error on SaveChanges() but have checked in the debugger and SiteID is definitely being assigned an int)

Sites

public class Sites
{
    [Key]
    public int SiteID { get; set; }
    public string Name { get; set; }
    public string Tags { get; set; }
    public string Description { get; set; }
    public string URI { get; set; }
}

CMSModels

public class CMSModels : DbContext
{
//public DbSet<ContentTypeModel> ContentType { get; set; }
    //public DbSet<LayoutModel> Layout { get; set; }
    //public DbSet<PageModel> Page { get; set; }
    //public DbSet<PageZoneModel> PageZone { get; set; }
    public DbSet<Sites> Site { get; set; }
    //public DbSet<ZoneContentModel> ZoneContent { get; set; }
    //public DbSet<ZoneTypeModel> ZoneType { get; set; }
}

HomeController:

private CMSModels models = new CMSModels();

public ActionResult Index()
{
    Sites site = new Sites { SiteID = 4, Name = "Name", Description = "Desc", Tags = "", URI = "" };

    models.Site.Add(site);
    models.SaveChanges();

    return View(models.Site.ToList());
}

I don't understand why I am getting this error, so any ideas would be appreciated.

If you need to see any other code please let me know.

Edit:

Just to extend this question a bit, it seems that this only happens on primary keys which are of the type int.

If we set the database column data type to nvarchar and the data types in the code to string it works fine.

Is this just a bug in Entity Framework 4, or are we just doing something wrong?

If we can't find a way to insert ints into a primary key column we may just have to use strings instead and validate them to make sure they are numeric.

解决方案

If you want a NO IDENTITY primary key column, so you set the id manually, you have 2 possible solutions.

1. Decorate your id propertie with the following Attributes (DatabaseGenerated...).

    public class Sites
    {
        [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int SiteID { get; set; }
        public string Name { get; set; }
        public string Tags { get; set; }
        public string Description { get; set; }
        public string URI { get; set; }
    }

2. Do it using the ModelBuilder in your DbContext.

    public class CMSModels : DbContext
    {
      //public DbSet<ContentTypeModel> ContentType { get; set; }
      //public DbSet<LayoutModel> Layout { get; set; }
      //public DbSet<PageModel> Page { get; set; }
      //public DbSet<PageZoneModel> PageZone { get; set; }
      public DbSet<Sites> Site { get; set; }
      //public DbSet<ZoneContentModel> ZoneContent { get; set; }
      //public DbSet<ZoneTypeModel> ZoneType { get; set; }

      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
          modelBuilder.Entity<Sites>().Property(r => r.SiteID) 
                       .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
      }
    }

这篇关于C#.NET 4.0的MVC插入与主键的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆