English 中文(简体)
ASP. NET MVC 3,Q服务器
原标题:ASP.NET MVC 3, SQL Server

首先,我向各位展示了我所做的事,现在的情况是存在的。 我在伙伴关系中的项目。 NET MVC 3.

这是表格业务的模式。

[Table("Business")]
public class Business
{
    [Key]
    public long? BusinessId { get; set; }

    public Int32 LegalTypeId { get; set; }
    public Int16 BusinessTypeId { get; set; }
    public Int64 OwnerId { get; set; }
    public Int32 MainIndustryFieldId { get; set; }
    public String NameNative { get; set; }
    public String NameEnglish { get; set; }
    public Byte[] Logo { get; set; }
    public DateTime CreateDate { get; set; }

    public virtual User Owner { get; set; }//It is Forign key with user table
}

[Table("User")]
public class User
{
    [Key]
    public Int64 UserId { get; set; }

    public String UserName { get; set; }
    public String LoweredUserName { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String Email { get; set; }
    public String LoweredEmail { get; set; }
    public String Password { get; set; }
    public String PasswordSalt { get; set; }
    public String PasswordQuestion { get; set; }
    public String PasswordAnswer { get; set; }
    public Boolean IsApproved { get; set; }
    public Boolean IsLocked { get; set; }
    public DateTime LastActivityDate { get; set; }
    public DateTime LastLoginDate { get; set; }
    public DateTime CreateDate { get; set; }

    public virtual Business Bussines { get; set; }
}

当我想补充信息时:

user.Business = new Business
{
    OwnerId = user.UserId,
    LegalTypeId = business.LegalTypeId,
    BusinessTypeId = business.BusinessTypeId,
    MainIndustryFieldId = business.MainIndustryFieldId,
    NameNative = business.NameNative,
    NameEnglish = business.NameEnglish,
    Logo = business.Logo,
    CreateDate = DateTime.Now
};
ctx.SaveChanges();

我有以下错误:

Cannot insert explicit value for identity column in table Business when IDENTITY_INSERT is set to OFF.

在数据库栏目中,选定身份识别业务为<代码>实体是。

Somebody can tell what to do.

问题回答

您能否保证,在您的EDMX模型中,您的<代码>BusinesssId将由数据库处理(property StoreGeneratedPattern = identity)? 另外,why 是你的主要关键不可否认(<>codelong?)是毫无意义的。 页: 1

“entergraph

<代码>Business Id不能空洞/null,因为你没有具体规定,它应由Sql服务器自动生成(Identity)。

由于你使用《法典》首先制作你的数据库,对以下几方面做了修改:

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long? BusinessId { get; set; }

优先于Model 在DbContext设立,并在文本中添加以下文字:

modelBuilder.Entity<Business>().HasRequired(m => m.Address).WithRequiredPrincipal(m => m.Business).WillCascadeOnDelete(true);




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签