小编Tor*_*ton的帖子

在Entity Framework中存储基于接口的关系

我有这个想要实现的域设计理念.我正在使用Entity Framework作为存储机制,我能看到这样做的唯一方法似乎并不理想.

我有很多域对象,其中许多都需要"标记".用户需要能够以某种方式"标记"它们中的每一个.

所以我想我会创建这个界面:

public interface IFlaggable
{
    [Required]
    Tally Tally { get; }
    Guid? TallyId { get; }
}
Run Code Online (Sandbox Code Playgroud)

和这些类:

public class Flag
{
    public Guid Id { get; internal set; }
    [Required]
    public Tally Tally { get; internal set; }
    public Guid TallyId { get; internal set; }
    [Required]
    public User Creator { get; internal set; }
    public Guid CreatorId { get; internal set; }
    [Required]
    public FlagIndication Indication { get; internal set; }
}

public class Tally …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

7
推荐指数
1
解决办法
946
查看次数

是否可以使用IList(Of IList(Of String))?

我正在尝试使用一个变量,它只是一个字符串列表的列表.我已经宣布如下:

Dim iRows As New List(Of List(Of String))
Run Code Online (Sandbox Code Playgroud)

然后我试图将它作为参数传递给另一个方法,我已经定义了如下方法:

Public Sub Import(ByVal Rows As IList(Of IList(Of String)))
    For Each Row As IList(Of String) In Rows
        ImportRow(Row)
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我尝试运行该代码时,我得到以下错误,它尝试将我的变量传递给我的方法.

用户代码未处理System.InvalidCastException消息="无法将类型为'System.Collections.Generic.List 1[System.Collections.Generic.List1 [System.String]]'的对象强制转换为'System.Collections.Generic.IList 1[System.Collections.Generic.IList1 [System.String]] "".

当我更改方法定义以使用类型而不是接口时,它可以工作.

Public Sub Import(ByVal Rows As List(Of List(Of String)))
    For Each Row As IList(Of String) In Rows
        ImportRow(Row)
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

那么,是否不可能以这种方式使用泛型和接口?只要我没有嵌套它就可以正常工作.

vb.net string generics interface list

1
推荐指数
1
解决办法
2842
查看次数

标签 统计

c# ×1

entity-framework ×1

generics ×1

interface ×1

list ×1

string ×1

vb.net ×1