【问题标题】:SQLite.Net - Don't know about System.Collections.Generic.List fault - Xamarin AndroidSQLite.Net - 不知道 System.Collections.Generic.List 错误 - Xamarin Android
【发布时间】:2015-11-28 12:30:41
【问题描述】:

我在这个问题上搜索了几个小时,他不知道能力是非常合乎逻辑的(因为能力还没有制作出来),但是你们知道如何解决这个问题吗?

这是我收到的错误消息: 不知道 System.Collections.Generic.List`1[pokedex.Ability]

我的课程: -能力

[Table ("Ability")]
public class Ability
{
    [PrimaryKey,AutoIncrement]
    public int Id { get; set; }

    public String name{ get; set; }

    [ManyToMany (typeof(PokemonAbility))]
    public List<Pokemon> pokemon{ get; set; }

    public Ability (String n)
    {
        name = n;
    }
}

-口袋妖怪:

[Table ("Pokemon")]
public class Pokemon
{
    [PrimaryKey,AutoIncrement]
    public int DBId { get; set; }

    public int id { get; set; }

    public String name{ get; set; }

    public String type{ get; set; }

    public String image{ get; set; }

    public int Attack{ get; set; }

    public int speed{ get; set; }

    public int sp_atk{ get; set; }

    public int sp_def{ get; set; }

    public int defense{ get; set; }

    public string height{ get; set; }

    public String weight{ get; set; }

    public int hp{ get; set; }

    public String description{ get; set; }

    [ManyToMany (typeof(PokemonAbility))]
    public List<Ability> abilities{ get; set; }

    methods and consttuctor...


}

口袋妖怪能力:

[Table ("PokemonAbility")]
public class PokemonAbility
{
    public PokemonAbility ()
    {
    }

    [ForeignKey (typeof(Pokemon))]
    public int PokemonId { get; set; }

    [ForeignKey (typeof(Ability))]
    public int AbilityId { get; set; }
}

我的数据库:

public class NormalDatabase
{

    private String pathToDatabase;

    public NormalDatabase ()
    {
        var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
        pathToDatabase = Path.Combine (documents, "db_adonet.db");
    }

    //aanmaken van de tabel => met objecten pokemon
    public void CreateDatabase ()
    {
        using (var conn = new SQLite.SQLiteConnection (pathToDatabase)) {
            conn.DropTable<Pokemon> ();
            conn.DropTable<Ability> ();
            conn.DropTable<PokemonAbility> ();
            conn.CreateTable<Pokemon> ();  //here he fails ofc
            conn.CreateTable<Ability> ();
            conn.CreateTable<PokemonAbility> ();
        }
    other methods
    }

提前致谢!

【问题讨论】:

    标签: c# android sqlite orm xamarin


    【解决方案1】:

    列表不是 SQLite 数据库值的有效类型。

    查看以下有效类型:

    
        public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks)
        {
            var clrType = p.ColumnType;
            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32)) {
                return "integer";
            } else if (clrType == typeof(UInt32) || clrType == typeof(Int64)) {
                return "bigint";
            } else if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal)) {
                return "float";
            } else if (clrType == typeof(String)) {
                int len = p.MaxStringLength;
                return "varchar(" + len + ")";
            } else if (clrType == typeof(DateTime)) {
                return storeDateTimeAsTicks ? "bigint" : "datetime";
                #if !NETFX_CORE
            } else if (clrType.IsEnum) {
                #else
            } else if (clrType.GetTypeInfo().IsEnum) {
                #endif
                return "integer";
            } else if (clrType == typeof(byte[])) {
                return "blob";
            } else if (clrType == typeof(Guid)) {
                return "varchar(36)";
            } else {
                throw new NotSupportedException ("Don't know about " + clrType); //Here the exception is thrown
            }
        }
    
    

    【讨论】:

      【解决方案2】:

      我认为这是因为您的能力表定义中的拼写错误。在 ManyToMany 属性中,您应该使用“Pokemon”而不是“Pokmemons”。此外,您的另一个表是“能力”而不是“能力”。如果没有帮助,请更新帖子...

      【讨论】:

      • 我更新了代码,还不行。一样的错。谢谢回复!
      • 在 manytomany 属性中仍然有“AbilityId”,但列的名称是“Id”。口袋妖怪表也是如此。我认为您不需要在属性中指定列 - 此外,如果稍后您想要更改列的名称,它将无法正确重构。为什么不直接写 [ManyToMany(typeof(PokemonAbility))] ?
      • 我在没有这些规范的情况下尝试过 ([ManyToMany(typeof(PokemonAbility))]) ,但它给出了同样的错误,如果我只是输入 Id en DBId,它不知道列表。
      • 好的,现在看起来它没有加载 sqlite 扩展。你是怎么添加的?你用nuget吗?尝试从项目中删除扩展和 sqlite-net 包,然后添加扩展 nuget。有关更多信息,请参阅此链接:stackoverflow.com/questions/23463849/…
      【解决方案3】:

      请参考异常类型:NotSupportedException

      请看这里:how to create collection (1:n) relation

      【讨论】:

      • 我认为 OP 确实根据您所指文章中的答案建立了关系(我的意思是,当然,第二个答案建议使用扩展 - 正是 op 使用的)。所以我不明白为什么你提供的链接回答了这个问题。
      猜你喜欢
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多