开发者

Active Record - HasAndBelongsToMany is not working

I have 3 tables in SQL Server Database.

I am using Active Record for .NET and I am having the problem that data in the UserAttachment table is not being saved when I create a new User including an attachment. It is giving me the following exception: "Cannot insert the value NULL into column 'UserID', table 'UsersDB.dbo.UsersAttachments'; column does not allow nulls. INSERT fails." The statement has been terminated.

I am assuming that UserID suppose is being filled automatically since that column is a PrimaryKey with AutoIncrement number generated.

In the User table data is saved but not in the UserAttachments

Please see the Table Name following the column names.

User -UserID -Name -LastName

UserAttachments -UserAttachmentID -UserID -AttachmentTypeID -Name -FilenamePath

AttachmentType -AttachmentTypeID -TypeName

    [PrimaryKey(PrimaryKeyType.Native, "UserID")]
    public int UserID{ get; set; }
    [Property(NotNull = false)]
    public string Name { get; set; }
    [Property(NotNull = false)]
    public string LastName{ get; set; }


    [HasAndBelongsToMany(
        typeof(Attachments.Attachments),
        Tab开发者_如何学JAVAle = "UserAttachments",
        ColumnKey = "UserAttachmentID",
        ColumnRef = "UserID",
        Cascade=ManyRelationCascadeEnum.All,
        Inverse = true,
        )
    ]
    public IList<Users.Attachments> UserAttachments  { get; set; }

Have you got an idea what I am doing wrong?

David


The first thing I can spot is you have ColumnRef and ColumnKey are the wrong way around.

ColumnKey is the name of the column where the ID of the object you are mapping from is stored. In this case a User. So ColumnKey should be set to "UserId"

ColumnRef is the ID which references the thing you are mapping to. So ColumnRef should be set to "UserAttachmentId"

Another problem I can spot is that the type specified in the attribute should be the type of the object in the collection. Your collection contains Users.Attachments, yet you have specified typeof(Attachments.Attachments)

Finally your Inverse=true says that your attachments "own" the Users. I do not know your app, but this seems counterintuitive.

As for which of the above issues is causing your problem, I am not entirely sure.

Some further documentation here http://www.castleproject.org/activerecord/documentation/trunk/usersguide/relations/hasandbelongs.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜