我在用 EF Core 2.1
如何在 EF Core 中映射一对一关系。我有Customer&Course域实体,其中一位客户将拥有一门课程。
这就是我的 Customer & CoursePOCO 的样子。
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CouseName { get; set; }
public virtual Customer Customer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 FluentAPI。
public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Parent> builder)
{
builder.HasKey(x => x.Customer.Id) //not …Run Code Online (Sandbox Code Playgroud) c# entity-framework-core asp.net-core ef-core-2.1 entity-framework-migrations
对于 SQL Server 中的数据库管理领域,我还很陌生。
下面是我的 SQL 代码,用于创建登录名和用户并授予权限
USE TestDb
GO
CREATE LOGIN [TestLogin]
WITH PASSWORD = N'123', DEFAULT_DATABASE = [TestDb],
CHECK_EXPIRATION = OFF, CHECK_POLICY = OFF
GO
CREATE USER SqlUser FOR LOGIN [TestLogin]
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA :: dbo to SqlUser
Run Code Online (Sandbox Code Playgroud)
这运行良好并创建用户/登录。他可以访问 dbo 架构。
但我需要在SCHEMA选项中指定多个模式。
所以我尝试了:
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA :: [dbo,app] to SqlUser
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
找不到架构“dbo;app”,因为它不存在或您没有权限。
如何在上述 SQL 模式中指定多个架构来授予用户访问权限?
谢谢!
根据定义,ref关键字必须在传递之前初始化.而out参数必须在从函数返回之前初始化.
以下是我的片段.
public void TestRef(ref string n)
{
}
public void TestOut(out string n)
{
n = "Hello"; //if I don't initialize, I gets compile time error. & That's right.
}
Run Code Online (Sandbox Code Playgroud)
现在在调用方法时.
string name;
TestOut(out name);//fine
TestRef(ref name) // why not throwing error.
Run Code Online (Sandbox Code Playgroud)
在尝试调用TestRef()时的上述调用中,我没有初始化name参数.但根据我的理解,ref参数必须在传递之前初始化.
它构建和运行没有错误.
我为我的DAL使用EF Core 2.1。这就是我的模型的样子
一个用户只能拥有一个角色:
// Role entity - kind of master
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
}
// User entity - every user will have one role
public class User
{
public int UserId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int RoleId { get; set; }
public bool IsActive { get; set; }
public …Run Code Online (Sandbox Code Playgroud) 我有ASP.Net Core 2.1 API。如下所示。
[HttpPost]
public async Task<IActionResult> Post([FromBody]string email) {
}
Run Code Online (Sandbox Code Playgroud)
当从邮递员调用时,我必须如下调用上述 API,并将标头设置为Content-Type = application/json
从应用程序调用相同的 API 时 Angular 8。
public send(email: string) {
//other tried option #1 let obj = JSON.stringify(email);
//other tried option #2 let obj = this.http.post(API.Url, {email});
/other tried option #3 let obj =
return this.http.post(API.Url, email).pipe(
retry(2)
);
}
Run Code Online (Sandbox Code Playgroud)
这引发了以下错误:-
415 不支持的媒体类型;
如果调用已更新标头:-
/* httpOptions = new HttpHeaders({
'Content-Type': 'application/json'
}*/
public send(email: string) {
return this.http.post(API.User, email , httpOptions).pipe( …Run Code Online (Sandbox Code Playgroud) 我正在.Net Core 3.1使用 Visual Studio 2019创建 WinForms 应用程序。
这就是我的应用程序的样子。
但是在单击表单 Form1.cs 上我没有看到设计器窗口。而当我创建相同的使用.Net Framework v4.7.1
我可以看到设计器窗口。
根据以下链接,使用 Visual Studio 2019,我不需要任何额外的配置/vsix 来执行此操作。
这是工具选项窗口的样子
看不到任何启用它的选项。
请检查并建议如何在使用 .Net Core 3.1 时获取设计器窗口并使用工具栏设计 WinForms 应用程序
谢谢!
我正在使用ASP.Net4.5.1和EF6.0 Code-First方法.
我意识到查询需要花费大量时间来执行.我使用EF Profiler检查查询并微调查询.
我用谷歌搜索并开始编译Linq.
这些是我得到的链接.
但是没有一个能解决我的问题.
假设我有以下查询.
public IEnumerable<Student> GetStudents()
{
using(DbContext db = new DbContext()
{
IQueryable<Student> query = (from c in db.Students
where c.Hobby== "Hockey"
select c);
IEnumerable<Student> students= query.toList<Student>();
return students;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我如何缓存查询计划或编译此Linq以提高性能?
还有一件事要问,EF比原生SQL慢吗?
我更喜欢使用EF/ORM,这是正确的选择吗?
我在很长一段时间内阅读了C#6.0中的异常过滤器
我试图在行动中使用它.但不知道为什么我得到以下错误.
CS1003语法错误,'何时'预期C_6.0
这就是我的代码片段的样子
using System;
using static System.Console;
namespace C_6._0
{
public class Program
{
static void Main(string[] args)
{
int val = 1;
try
{
WriteLine("Enter value :");
val = int.Parse(ReadLine());
}
catch (Exception ex) if (val == 0)
{
WriteLine("Input Invalid");
}
catch (Exception ex)
{
WriteLine(ex.Message);
}
ReadLine();
}
}}
Run Code Online (Sandbox Code Playgroud)
我查看了C#的版本,它在我的visual studio中的6.0
属性 - >构建 - >高级构建设置
谢谢.
我有一个 ASP.Net Core WebAPI,我得到以下要求
我有 2 种方法来处理 HTTP GET 请求,第一个方法是通过 id( string) 来处理 GetCustomer,另一种方法是通过 email( string) 来处理 GetCustomer。
//GET : api/customers/2913a1ad-d990-412a-8e30-dbe464c2a85e
[HttpGet("{id}")]
public async Task<ActionResult<Customer>> GetCustomer([FromRoute]string id)
{
}
// GET: api/customers/myemail@gmail.com
[HttpGet("{name}")]
public async Task<ActionResult<Customer>> GetCustomerByEmail([FromRoute]string email)
{
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个端点时,我得到异常:
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.
Run Code Online (Sandbox Code Playgroud)
这是非常明显和理解的。
通过在路由中附加/前置一些字符串可以轻松解决这个问题,例如
/api/customer/{id}
/api/customer/emai/{id}
Run Code Online (Sandbox Code Playgroud)
然而,我不太相信这个方法,但是在谷歌搜索上我得到了下面的链接
但这家伙有一个参数为 int,而另一个参数为字符串,因此路由约束被拯救。
但是,我看到有些人在同一篇文章上发帖/建议添加[Route("")],但我不明白这个属性有什么用?
afaik,Route("")和HTTPGet("")//任何 HttpVerb 都有相同的目的吗?
无论如何,我怎样才能优雅地处理我的要求呢?
我已经AWS Lambda Project在C#(NOT Serverless Application)中创建了
我在aws-lambda-tools-defaults.json下面定义了一个环境变量
"environment-variables": {
"my-api": "http://myapihost.com/api/attendance-backfill"
}
Run Code Online (Sandbox Code Playgroud)
在Function.cs中,按如下所示获取值
var apiUrl = Environment.GetEnvironmentVariable("my-api").ToString();
Run Code Online (Sandbox Code Playgroud)
但是它总是像null。
如何设置和获取环境变量?
谢谢!
根据评论。
c# environment-variables amazon-web-services aws-lambda asp.net-core
c# ×8
asp.net-core ×5
asp.net ×2
ef-core-2.1 ×2
.net-core ×1
angular ×1
angular8 ×1
asp.net-mvc ×1
aws-lambda ×1
c#-6.0 ×1
database ×1
entity-framework-migrations ×1
media-type ×1
out ×1
ref ×1
sql ×1
sql-server ×1
winforms ×1