0

ForeignKeyAttribute 在接口上不起作用?

public class Client: CompanyForeignKeyMapper
{
     public int Client;
}

public class CompanyForeignKeyMapper
{
     [ForeignKeyAttribute("Company")]
     int ClientId;
}

此代码工作正常。但是当我将 CompanyForeignKeyMapper 类更改为接口时

public interface ICompanyForeignKeyMapper


public class Client: ICompanyForeingKeyMapper --> changed to interface

该属性似乎不起作用。

4

1 回答 1

0

当您在第二个示例中声明 Client 类时,它不再继承自 CompanyForeignKeyMapper 类,因此它将没有 ClientId 字段。如果您希望它从 CompanyForeignKeyMapper 继承并实现 ICompanyForeingKeyMapper 接口,请像这样声明它:

public class Client: CompanyForeignKeyMapper, ICompanyForeingKeyMapper

或者,您可以将 ClientId 定义为接口的字段,在这种情况下,客户端类必须实现它并可以向其中添加自己的 ForeignKeyAttribute:

public interface ICompanyForeignKeyMapper
{
    int ClientId;
}


public class Client: ICompanyForeignKeyMapper
{
    [ForeignKeyAttribute("Company")]
    int ClientId;
}
于 2012-11-18T19:00:45.230 回答