创建基于OData的Web API - Knowledge Builder API, Part III:Write Model
2021-05-16 17:27
标签:开始 sch false member exception cep column 文件 typeof 在前两篇文章 步骤如下: 1. 创建Models文件夹,并在该文件夹中加入一个数个Class。 Knowledge Category定义,代码如下: 基类BaseModel,代码如下: Knowledge的Model,代码如下: 最后加入DataContext,代码如下: 2. 如果Controller文件夹尚未创建,则创建一个,并在其中创建Knowledge的Controller 完整代码如下: 3. 修改Startup 4. 在项目的根目录下执行 5. 这时,打开浏览器,访问 http://localhost:5000/odata/$metadata 会成功拿到一下文件: 创建基于OData的Web API - Knowledge Builder API, Part III:Write Model 标签:开始 sch false member exception cep column 文件 typeof 原文地址:https://www.cnblogs.com/alvachien/p/11809136.htmlusing System;
namespace knowledgebuilderapi.Models {
public enum KnowledgeCategory: Int16 {
Concept = 0,
Formula = 1,
}
}
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace knowledgebuilderapi.Models {
public abstract class BaseModel {
[Column("CreatedAt")]
public DateTime CreatedAt { get; set; }
[Column("ModifiedAt")]
public DateTime ModifiedAt { get; set; }
}
}
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace knowledgebuilderapi.Models
{
[Table("Knowledge")]
public class Knowledge : BaseModel
{
[Key]
public Int32 ID { get; set; }
[Required]
[Column("ContentType")]
public KnowledgeCategory Category { get;set; }
[Required]
[MaxLength(50)]
[ConcurrencyCheck]
[Column("Title", TypeName = "NVARCHAR(50)")]
public string Title { get;set; }
[Required]
[Column("Content")]
public string Content { get;set; }
[Column("Tags")]
public string Tags { get; set; }
}
}
using System;
using Microsoft.EntityFrameworkCore;
namespace knowledgebuilderapi.Models
{
public class kbdataContext : DbContext
{
public kbdataContext(DbContextOptions
using System;
using Microsoft.AspNet.OData;
using Microsoft.EntityFrameworkCore;
using knowledgebuilderapi.Models;
using System.Linq;
namespace knowledgebuilderapi.Controllers {
public class KnowledgeController : ODataController {
private readonly kbdataContext _context;
public KnowledgeController(kbdataContext context)
{
_context = context;
}
[EnableQuery]
public IQueryable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Batch;
using knowledgebuilderapi.Models;
using Microsoft.AspNetCore.Routing;
namespace knowledgebuilderapi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public string ConnectionString { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
this.ConnectionString = Configuration["KBAPI.ConnectionString"];
services.AddDbContext
cd knowledgebuilderapi
dotnet run
edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
edmx:DataServices>
Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="knowledgebuilderapi.Models">
EntityType Name="Knowledge">
Key>
PropertyRef Name="ID"/>
Key>
Property Name="ID" Type="Edm.Int32" Nullable="false"/>
Property Name="Category" Type="knowledgebuilderapi.Models.KnowledgeCategory" Nullable="false"/>
Property Name="Title" Type="Edm.String" Nullable="false" MaxLength="50"/>
Property Name="Content" Type="Edm.String" Nullable="false"/>
Property Name="Tags" Type="Edm.String"/>
Property Name="CreatedAt" Type="Edm.DateTimeOffset" Nullable="false"/>
Property Name="ModifiedAt" Type="Edm.DateTimeOffset" Nullable="false"/>
EntityType>
EnumType Name="KnowledgeCategory" UnderlyingType="Edm.Int16">
Member Name="Concept" Value="0"/>
Member Name="Formula" Value="1"/>
EnumType>
EntityContainer Name="Container">
EntitySet Name="Knowledges" EntityType="knowledgebuilderapi.Models.Knowledge">
Annotation Term="Org.OData.Core.V1.OptimisticConcurrency">
Collection>
PropertyPath>TitlePropertyPath>
Collection>
Annotation>
EntitySet>
EntityContainer>
Schema>
edmx:DataServices>
edmx:Edmx>
文章标题:创建基于OData的Web API - Knowledge Builder API, Part III:Write Model
文章链接:http://soscw.com/index.php/essay/86335.html