C# 具有实体属性而不是ID的ServiceStack.Redis搜索列表
我试图通过Servicestack.Redis为MVC应用程序使用Redis缓存 Redis仅适用于键字段(默认Id)。我有以下课程C# 具有实体属性而不是ID的ServiceStack.Redis搜索列表,c#,entity-framework,
servicestack.redis,C#,Entity Framework,
servicestack.redis,我试图通过Servicestack.Redis为MVC应用程序使用Redis缓存 Redis仅适用于键字段(默认Id)。我有以下课程 [DataContract] public partial class Author_Book { public Author_Book() { } public Author_Book(int id, string Title, int AuthorID) { this.Id = id; this.Titl
[DataContract]
public partial class Author_Book
{
public Author_Book() { }
public Author_Book(int id, string Title, int AuthorID)
{
this.Id = id;
this.Title = Title;
this.AuthorId = AuthorID;
}
[DataMember]
public int Id { get; set; }
[DataMember]
public int AuthorId { get; set; }
[DataMember]
public string Title { get; set; }
public virtual Author Author { get; set; }
}
[DataContract]
public partial class Author
{
public Author()
{
this.Author_Book = new HashSet<Author_Book>();
}
public Author(int id, string name)
{
this.Id = id;
this.Name = name;
this.Author_Book = new HashSet<Author_Book>();
}
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public virtual ICollection<Author_Book> Author_Book { get; set; }
}
[DataContract]
公共部分类作者书
{
公共作者_Book(){}
公共作者书(int-id,字符串标题,int-authord)
{
这个.Id=Id;
这个.Title=Title;
this.AuthorId=AuthorId;
}
[数据成员]
公共int Id{get;set;}
[数据成员]
公共int AuthorId{get;set;}
[数据成员]
公共字符串标题{get;set;}
公共虚拟作者{get;set;}
}
[数据合同]
公共部分类作者
{
公共作者()
{
this.Author_Book=new HashSet();
}
公共作者(int-id,字符串名)
{
这个.Id=Id;
this.Name=Name;
this.Author_Book=new HashSet();
}
[数据成员]
公共int Id{get;set;}
[数据成员]
公共字符串名称{get;set;}
[数据成员]
公共虚拟ICollection作者{get;set;}
}
和下面的函数进行测试
public static void ListTest()
{
// configure Redis to use AuthorID as ID field for Author_Book entities
ServiceStack.ModelConfig<Author_Book>.Id(x => x.AuthorId);
PooledRedisClientManager redisManager = new PooledRedisClientManager("localhost:6379");
Author auth = new Author(1, "Author-1");
Author auth2 = new Author(2, "Author-2");
Author_Book ab1 = new Author_Book(1, "Book-1", 1);
Author_Book ab2 = new Author_Book(2, "Book-2", 2);
Author_Book ab3 = new Author_Book(3, "Book-3", 2);
IList<Author_Book> retList;
using(IRedisClient rc = redisManager.GetClient())
{
// store Authors
rc.Store<Author>(auth);
rc.Store<Author>(auth2);
// store Author Books
rc.Store<Author_Book>(ab1);
rc.Store<Author_Book>(ab2);
rc.Store<Author_Book>(ab3);
// Get data back from redis
List<int> ids = new List<int>();
ids.Add(2);
retList = rc.GetByIds<Author_Book>(ids);
}
foreach(Author_Book ab in retList)
{
Console.WriteLine(ab.Title);
}
Console.Read();
}
公共静态无效列表测试()
{
//配置Redis以使用AuthorID作为Author\u Book实体的ID字段
ServiceStack.ModelConfig.Id(x=>x.AuthorId);
PooledRedisClientManager redisManager=新的PooledRedisClientManager(“本地主机:6379”);
Author auth=新作者(1,“Author-1”);
authorAuth2=新作者(2,“Author-2”);
作者书ab1=新作者书(1,“书-1”,1);
作者书ab2=新作者书(2,“书-2”,2);
作者书ab3=新作者书(3,“书-3”,2);
IList retList;
使用(IRedisClient rc=redisManager.GetClient())
{
//商店作者
rc.商店(auth);
rc.商店(auth2);
//存储作者书籍
钢筋混凝土仓库(ab1);
钢筋混凝土仓库(ab2);
钢筋混凝土仓库(ab3);
//从redis获取数据
列表ID=新列表();
添加(2);
retList=rc.GetByIds(ids);
}
foreach(作者/书名列表中的ab)
{
控制台写入线(ab.标题);
}
Console.Read();
}
我想做的是
Redis
配置为使用AuthorID
作为Author\u Book实体的关键字段Book-3
,预期结果是Book-2
和Book-3
我猜rc.Store(ab3)
正在覆盖上一条记录,因为ab2
和ab3
具有相同的AuthorID
如何实现
我想存储单独的实体列表,而不是图形,以便以后可以单独更新/删除它们