could not insert select SCOPE_IDENTITY() fluent nh

2019-07-24 19:30发布

I have an error exception: "could not insert select SCOPE_IDENTITY()". After certain hours of googling, I found that I have a mistake in my Mapping files. I tried all the possible solutions, but the error keeps appearing.
My mapping files:

public sealed class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Table("dbo.Employee");

        Id(x => x.Id).Column("EmployeeId");
        Map(x => x.Name);
        Map(x => x.Job);
        HasMany(x => x.Phones).KeyColumn("EmployeeId").Table("dbo.Phone")
            .Inverse()
            .Cascade.All();
    }
}
public sealed class PhoneMap : ClassMap<Phone>
{
    public PhoneMap()
    {
        Table("dbo.Phone");
        Id(x => x.Id).Column("PhoneId");
        Map(x => x.PhoneNumber);
        Map(x => x.PhoneType);
        Map(x => x.EmployeeId);
        References(x => x.Employee).Column("EmployeeId").Not.Nullable();
    }
}

The problem occurs in Session.SaveOrUpdate().
Where did I wrong?

4条回答
Evening l夕情丶
2楼-- · 2019-07-24 20:00

My experience tells me that usually when select SCOPE_IDENTITY() error happens is due to an incorrect mapping.

Make sure all the Mapped fields match the ones in database. If they are nullable or not, if you have mappings for ALL of them, etc.

Then as you pointed the referenced many-to-one relationship is mapped as:

References<TableRelated>(x => x.PropertyRelated, "fieldId");

So it should be References<Employee>(x => x.Employee, "EmployeeId");

but that could be another issue (not sure if it generates the same scope_identity error)

查看更多
地球回转人心会变
3楼-- · 2019-07-24 20:04

It seems that no body answered to my question...
No need. I found the answer by myself:

public sealed class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Table("dbo.Employee");

        Id(x => x.Id).Column("EmployeeId");
        Map(x => x.Name);
        Map(x => x.Job);
        HasMany(x => x.Phones).KeyColumn("EmployeeId").Table("dbo.Phone").Inverse().Cascade.AllDeleteOrphan();
    }
}
public sealed class PhoneMap : ClassMap<Phone>
{
    public PhoneMap()
    {
        Table("dbo.Phone");
        Id(x => x.Id).Column("PhoneId");
        Map(x => x.PhoneNumber);
        Map(x => x.PhoneType);
        Map(x => x.EmployeeId);
        References(x => x.Employee, "EmployeeId");
    }
}
查看更多
\"骚年 ilove
4楼-- · 2019-07-24 20:06

I have the same error. Maybe it will be useful for community.

I've found a root of an occurred exception: check your "Id" column in the table. The 'Id' column should be incremented. Under the creation a table it is necessary to set IDENTITY to unique field like that:

CREATE TABLE dbo.aTable (ID_User INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1), Name VARCHAR(30)) END
查看更多
来,给爷笑一个
5楼-- · 2019-07-24 20:21

I have the same problem. Turns out, I am saving more than the characters allowed on a specified field of my table. It is declared as:

(varchar(30), null)

And I am saving 31 characters and that same message error occurs. I just adjusted my characters length and everything went to normal.

查看更多
登录 后发表回答