如何在ASP.Net WebAPI模型中引用作为标识列的foriegnkey

时间:2015-10-08 17:19:12

标签: c# entity-framework asp.net-web-api entity-framework-4 ef-code-first

这就是我的ASP.Net REST Web API模型的样子,它们的DataTransferObject模型只是这些模型的副本而没有任何属性。我没有提到ModelDto类,因为它们看起来与模型非常相似。我想用c#中的Entity Framework Code First方法填充数据库。

        class Customer
        {
            public int id { get; set; } // this an identity column in db

            public string name { get; set; }

            public string UserId { get; set; }

            public string Source { get; set; }

            public DateTime RegisteredOn { get; set; }
        }

        // Note: Other than identity column , The combinmation of UserId and Source is unique.

        class Ticket
        {
            public int id { get; set; }   // this an identity column in db and its ticketid which is unique

            [ForeignKey("Customer")]
            public int id { get; set; } // i am thinking this should refer the identity column of the Customer class since its unique. but if i refer the foreign key as "id" , then i will have two properties in this class one for its own identity column and other one referring to the Customer class.

            public string Description { get; set; } // ticket problem description

            public DateTime CreatedOn { get; set; }
        }


        class TicketComment
        {
             public int id { get; set; }   // this an identity column in db and its commentid which is unique

             [ForeignKey("Ticket")]
             public int id { get; set; } // i am thinking this should refer the identity column of the Ticket class since its unique. but if i refer the foreign key as "id" , then i will have two properties in this class one for its own identity column and other one referring to the tTicket class.


             public string Comment { get; set; } // user may add a comment or notes to existing ticket to provide more info or asking for status     

        }

        class TicketUpdates
        {
            public int id { get; set; }   // this an identity column in db and its updateid which is unique

            [ForeignKey("Ticket")]
            public int id { get; set; } // i am thinking this should refer the identity column of the Ticket class since its unique. but if i refer the foreign key as "id" , then i will have two properties in this class one for its own identity column and other one referring to the tTicket class.


            public string Status { get; set; } // agent may add a status to existing ticket along with some notes

        }

我在这里有三个问题:

当当前表或实体已经具有同名的标识列时,如何引用其他表/实体的标识列。(因为我使用的是实体框架,所以它总是创建一个带有标识列名的表as" id"。)

我们如何在REST Web API的一次调用中检索客户记录以及每个客户的门票,每张门票的评论以及每张门票的更新?我的GET方法应如何在 CustomerController

中显示

我应该查看除标识列字段以外的其他字段来引用外键吗?

我查看了一些现有帖子,但我的方案看起来有点不同。

0 个答案:

没有答案