我正在尝试实现一种通用GetById(T id)方法,该方法将满足可能具有不同ID类型的类型.在我的例子中,我有一个实体,其ID类型int和类型之一string.
但是,我一直收到错误,我不明白为什么:
类型'int'必须是引用类型才能在方法IEntity的泛型类型中将其用作参数'TId'
实体接口:
为了满足我的域名模型,这些模型可以具有Id类型int或string.
public interface IEntity<TId> where TId : class
{
TId Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
实体实施:
public class EntityOne : IEntity<int>
{
public int Id { get; set; }
// Other model properties...
}
public class EntityTwo : IEntity<string>
{
public string Id { get; set; }
// Other model properties...
}
Run Code Online (Sandbox Code Playgroud)
通用存储库接口:
public interface IRepository<TEntity, TId> where TEntity : class, IEntity<TId>
{
TEntity …Run Code Online (Sandbox Code Playgroud) 背景:
我刚刚开始使用 Azure CosmosDB 并尝试使用实体框架 CosmosDB 提供程序。我创建了一个简单的 API 以及 Swagger API 文档,它将在单个实体上运行。
到目前为止我已经做了以下事情:
问题:
当尝试通过 Swagger 调用我的 POST 端点以在数据库中创建新记录时,出现以下错误:
Microsoft.Azure.Cosmos.CosmosException :响应状态代码不指示成功:BadRequest (400);子状态:1001;活动ID:fe27e816-173c-433e-8699-e9e49e01b96f;原因: (消息: {"Errors":["从文档中提取的 PartitionKey 与标头中指定的不匹配"]}
从以下教程和文档来看,我收到此错误的原因并不明显!任何正确方向的指示将不胜感激!
可能有用的片段可以帮助别人诊断我哪里出了问题:
将 Cosmos DB 注册到服务容器:
var settings = new CosmosDbOptions();
configuration.GetSection(CosmosDbOptions.SECTION_NAME)
.Bind(settings);
services.AddDbContext<DatabaseContext>(options =>
{
options.UseCosmos(
accountEndpoint: settings.EndpointUri,
accountKey: settings.PrimaryKey,
databaseName: settings.DatabaseName
);
});
Run Code Online (Sandbox Code Playgroud)
实体:
public class Client : Entity, IGuidIdentifier, IAggregateRoot
{ …Run Code Online (Sandbox Code Playgroud) 我试图用本地JSON信息填充JQM ListView.但是,不会创建任何列表项.任何帮助,将不胜感激.这是我的代码:
JSON文件结构:
[
{
"name" : "test"
"calories" : "1000"
"fat" : "100"
"protein" : "100"
"carbohydrates" : "800"
},
{
"name" : "test2"
"calories" : "10000"
"fat" : "343"
"protein" : "3434"
"carbohydrates" : "4343"
}
]
Run Code Online (Sandbox Code Playgroud)
HTML:
<div data-role="page" data-title="Search" id="searchPage">
<ul data-role="listview" data-inset="true" id="searchFood">
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
(更新)
$(document).on("pageinit", "#searchPage", function(){
$.getJSON("../JS/food.json", function(data){
var output = '';
$.each(data, function(index, value){
output += '<li><a href="#">' +data.name+ '</a></li>';
});
$('#searchFood').html(output).listview("refresh");
});
});
Run Code Online (Sandbox Code Playgroud)