Xamarin形成SQLite关系 [英] Xamarin forms SQLite relation
问题描述
我现在确实已经搜索了一段时间,但是找不到并理解它是如何工作的.我将以Xamarin形式创建一个应用程序,并从SQLite开始.我需要为应用程序中的每个主要项目都具有唯一的列表项.
I have really been searching around for a while now but cannot find and understand how this do work. I am going to create an app in Xamarin forms and starting with SQLite. I need to have unique list items for each Main item in the app.
例如,我有一个包含物品的列表.当我在列表中选择一个项目时,将弹出一个新页面并显示该项目的项目.
For example, I am having a list with items. When I am selecting an item in the list a new page will pop up and display the items of that item.
因此,从我的角度来看,我需要两个具有相互关系的SQLite表.
So from my point of view I am in need of two SQLite tables with relations between.
这是包含所有配置文件的主表
[Table("Profiles")]
public class ProfileItems
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string ProfileName { get; set; }
public string ProfileRace { get; set; }
public string iconn = "icon.png";
public string ProfileIcon { get; set; }
public DateTime BDay { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<LoggItems> Loggs { get; set; }
}
这是每个配置文件的logg表,对于每个配置文件应该是唯一的
[Table("Loggs")]
public class LoggItems
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
[ForeignKey(typeof(ProfileItems))]
public int ProfileId { get; set; }
}
添加类似的项目
public class ProfileDatabase
{
readonly SQLiteAsyncConnection database;
public ProfileDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<ProfileItems>().Wait();
database.CreateTableAsync<LoggItems>().Wait();
}
//Profile
public Task<List<ProfileItems>> GetProfileAsync()
{
return database.Table<ProfileItems>().ToListAsync();
}
public Task<ProfileItems> GetProfileAsync(int id)
{
return database.Table<ProfileItems>().Where(i => i.Id == id).FirstOrDefaultAsync();
}
public Task<int> SaveProfileAsync(ProfileItems profileItems)
{
if (profileItems.Id != 0)
{
return database.UpdateAsync(profileItems);
}
else
{
return database.InsertAsync(profileItems);
}
}
public Task<int> DeleteProfileAsync(ProfileItems profileItems)
{
return database.DeleteAsync(profileItems);
}
//Logg
public Task<List<LoggItems>> GetLoggAsync()
{
return database.Table<LoggItems>().ToListAsync();
}
public Task<LoggItems> GetLoggAsync(int id)
{
return database.Table<LoggItems>().Where(i => i.Id == id).FirstOrDefaultAsync();
}
public Task<int> SaveLoggAsync(LoggItems loggItems)
{
if (loggItems.Id != 0)
{
return database.UpdateAsync(loggItems);
}
else
{
return database.InsertAsync(loggItems);
}
}
public Task<int> DeleteLoggAsync(LoggItems loggItems)
{
return database.DeleteAsync(loggItems);
}
}
Logg和Profile列表/表都可以工作,但是两者之间没有任何关系,因此Logg在所有Profile中都显示相同的内容.
Both Logg and Profile list/tables do work but they do not have any relations between so the loggs show the same in all profile.
请帮助我该怎么做.
非常感谢.
推荐答案
如何使用Linq并加入关系.
1 .- .首先,您必须添加名称空间:
How about to use Linq and join the relationships.
1.- First you have to add the namespace:
using System.Linq;
2 .- .将ProfileItems类中的属性更改为 IEnumerable
2.- Change the property in the class ProfileItems to be a IEnumerable
[OneToMany(CascadeOperations = CascadeOperation.All)]
public virtual IEnumerable<LoggItems> Loggs { get; set; }
3 .- .这是将Logg与配置文件项结合在一起的方法.
3.- This is the method to join the loggs with the profile items.
var profiles = await GetProfileAsync();
var loggs = await GetLoggAsync();
var query = from p in profiles
join l in loggs on p.Id equals l.ProfileId into list
select new ProfileItems
{
Id = p.Id,
ProfileIcon = p.ProfileIcon,
ProfileName = p.ProfileName,
ProfileRace = p.ProfileRace,
BDay = p.BDay,
Loggs = list
};
这篇关于Xamarin形成SQLite关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!