[英]Error when referencing entities from ASP.Net MVC project in Azure Function project
I have a ASP.Net MVC project that contains my db entities and my context uses IdentityDbContext applicationuser我有一个包含我的 db 实体的 ASP.Net MVC 项目,我的上下文使用 IdentityDbContext applicationuser
public class MyprojectContext : IdentityDbContext<ApplicationUser>
everything works fine connecting and fetching database data!一切正常连接和获取数据库数据!
I also have a azure functions project to perform work and I need to access the db (using entity framework) and fetch different entity data.我还有一个 azure 函数项目来执行工作,我需要访问数据库(使用实体框架)并获取不同的实体数据。 So I referenced the MVC app in the Azure Function app so I could have all the entities/classes.
所以我在 Azure Function 应用程序中引用了 MVC 应用程序,这样我就可以拥有所有实体/类。
But when I try and fetch data into one of the entities 'YogaSpaceEvent' I get the error below, if I create my own entity in the Azure Function code (see code below) it works fine and fetches the data.但是,当我尝试将数据提取到实体“YogaSpaceEvent”之一时,我收到以下错误,如果我在 Azure 函数代码(请参阅下面的代码)中创建自己的实体,它可以正常工作并提取数据。
I'm guessing it might have something to do with each member fetching data in the MVC app using我猜这可能与每个成员使用 MVC 应用程序获取数据有关
IdentityDbContext<ApplicationUser>
and in the Azure Function app I'm only using DbContext????而在 Azure Function 应用中,我只使用了 DbContext???
System.Data.Entity.ModelConfiguration.ModelValidationException: 'One or more validation errors were detected during model generation: PostEventEmail.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined.
System.Data.Entity.ModelConfiguration.ModelValidationException: '在模型生成过程中检测到一个或多个验证错误:PostEventEmail.IdentityUserLogin:: EntityType 'IdentityUserLogin' 没有定义键。 Define the key for this EntityType.
定义此 EntityType 的键。
namespace PostEventEmail
{
public static class PostEventEmail
{
[FunctionName("PostEventEmail")]
public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
YogaSpaceEvent newEvent = new YogaSpaceEvent(); // references the Asp.Net MVC app entity
using (var dbContext = new YogabandyContext())
{
var ybEvents = dbContext.YogaSpaceEvent.FirstOrDefault();
log.Info("event id = " + ybEvents.YogaSpaceEventId);
}
}
// if I uncomment this and use it, it works fine and fetches data
//public class YogaSpaceEvent
//{
// public int YogaSpaceEventId { get; set; }
//}
public partial class YogabandyContext : DbContext
{
public YogabandyContext()
: base("Server=tcp:yoga2017.database.windows.net,1433;Initial Catalog=Yoga2017;Persist Security Info=False;User ID=chuckd;Password=**********!;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;")
{
}
public DbSet<YogaSpaceEvent> YogaSpaceEvent { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//base.OnModelCreating(modelBuilder);
}
}
}
} }
According to your description, I created my Asp.Net MVC application with ASP.NET Identity to check this issue.根据您的描述,我使用 ASP.NET Identity 创建了我的 Asp.Net MVC 应用程序来检查此问题。 I created the
YogaSpaceEvent
in my MVC project and defined the DBContext as follows:我在我的 MVC 项目中创建了
YogaSpaceEvent
并定义了 DBContext 如下:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<YogaSpaceEvent> YogaSpaceEvent { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public ApplicationDbContext(string connectionString) : base(connectionString, throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
public class YogaSpaceEvent
{
public int YogaSpaceEventId { get; set; }
public string YogaSpaceEventName { get; set; }
public DateTime CreateTime { get; set; }
}
For my Azure Function, I just referenced the MVC project and use the ApplicationDbContext
for accessing entities from my database as follows:对于我的 Azure 函数,我只是引用了 MVC 项目并使用
ApplicationDbContext
来访问我的数据库中的实体,如下所示:
public static void Run([TimerTrigger("*/20 * * * * *")]TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
var connString=ConfigurationManager.AppSettings["sqldb-connectionstring"];
using (var dbContext = new ApplicationDbContext(connString))
{
var ybEvents = dbContext.YogaSpaceEvent.FirstOrDefault();
log.Info("event id = " + ybEvents?.YogaSpaceEventId);
}
}
local.settings.json本地设置.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=AccountName;AccountKey=AccountKey",
"AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=AccountName;AccountKey=AccountKey",
"sqldb-connectionstring": "Data Source=.\\sqlexpress;Initial Catalog=DefaultConnection;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
}
It could work on my side.它可以在我身边工作。 Here is my code sample , you could refer to it.
这是我的代码示例,您可以参考它。 Moreover, your error is strange, you need to check your code or provide a sample project for us to reproduce this issue.
而且,您的错误很奇怪,您需要检查您的代码或提供示例项目供我们重现此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.