could not insert select SCOPE_IDENTITY() fluent nhibernate one to many

自古美人都是妖i 提交于 2019-12-24 04:22:14

问题


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?


回答1:


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");
    }
}



回答2:


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:


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



回答4:


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.



来源:https://stackoverflow.com/questions/25096975/could-not-insert-select-scope-identity-fluent-nhibernate-one-to-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!