我正在尝试执行以下操作:
public class class1
{
public int Id {get;set;}
[ForeignKey("Class2")]
public int Class2Id {get;set;}
public virtual Class2 Class2 {get;set;}
}
public class class2
{
public int Id { get; set;}
[Required]
public virtual int Class1Id {get;set;}
[Required]
[ForeignKey("Class1Id")]
public Class1 Class1 {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
但是,每次我尝试迁移我的数据库时,都会收到以下错误:
Class1_Class2_Target :: Multiplicity在关系'Class2_Class1'中的角色'Class2_Class1_Target'中无效.由于"从属角色"属性不是关键属性,因此从属角色的多重性的上限必须为"*".
这可能是什么问题?
c# database entity-framework ef-code-first entity-framework-6
我正在尝试为我的Web Api自动生成帮助文档.
但是我在一个特定方法上看到无法生成样本请求,如图所示:

以下是参数请求参数:

为什么无法生成样本请求格式?
基本上我正在尝试访问上传到Azure上的web api.
我已将我的CORS属性添加到我的WebApiConfig类:
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("http://localhost:51407", "*", "*");
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)
但是每当我发送一个正确包含的Ajax请求(使用angularJS)时
Origin: http://localhost:51407
Run Code Online (Sandbox Code Playgroud)
我收到错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:51407' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
可能是什么问题?
我有一个包含Products的数据库。这些产品已分类,具有类别和子类别。
例如:
Product p1 = new Product()
{
Category = Category.Fruit,
Subcategory = Subcategory.Apple
};
Run Code Online (Sandbox Code Playgroud)
我的问题是我想根据类别限制子类别。
下面的示例应该是不可能的:
Product p2 = new Product()
{
Category = Category.Fruit,
Subcategory = Subcategory.Cheese
};
Run Code Online (Sandbox Code Playgroud)
此外,我希望能够返回一个字符串数组(匹配每个Category枚举),每个字符串都有一个对应子类别的数组。
我已经思考了一段时间,但没有提出任何建议,也没有在线找到任何解决方案。
有什么建议?
基本上我已经创建了一个类库项目,其中包含一个数据访问存储库,供其他项目使用.我添加了EF6包并启用了迁移.我在app.config中的连接字符串如下所示:
<connectionStrings>
<add name="Pbn" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Pbn.mdf;Initial Catalog=LM.DataAccess;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
(项目名称为LM.DataAccess).
当我运行update-database命令时,我收到以下错误:
发生文件激活错误.物理文件名'\ _PBn.mdf'可能不正确.诊断并更正其他错误,然后重试该操作.CREATE DATABASE失败.无法创建列出的某些文件名.检查相关错误.
这可能是什么问题?
我正在做的事情:
首先,我的HTML看起来如下:
<form action="http://example.com/upload_photo_iframe.html" preview_div="upload_photo_div" submit_button="submit_btn" upload_field="photo_upload" target="photo_target" enctype="multipart/form-data" method="POST" id="uploadfile" name="uploadfile">
<input type="hidden" value="CSRF_iframe_photo_upload" name="csrfname">
<input type="hidden" value="3350427f0f068509081a09e283607214001b6912843ffb937b934208c91d9041c88faf0e66df4f3898ef202a34b669647f5b3fd9a2122e389acd3f53c33fc88b" name="csrftoken">
<label class="file-upload">
<p id="upload_text">Click Browse to choose a file then click Upload</p>
<input type="file" class="file" id="photo_upload" name="photo_upload" accept="image/*">
</label>
<input type="hidden" value="true" name="makeLargeThumb">
<input type="hidden" value="p" name="size">
<input type="hidden" value="P" name="type">
<input type="hidden" value="5120000" name="MAX_FILE_SIZE">
<input type="hidden" value="0" name="album_id" class="album_id_hidden">
<input type="submit" class="grey_btn" id="upload_btn" value="Upload" name="submit_btn">
<input type="hidden" name="callback" value="document.forms.uploadfile.handleReceive"><input type="hidden" name="fieldName" value="photo_upload"></form>
Run Code Online (Sandbox Code Playgroud)
我发布了整个文件,文件输入是以下部分:
<input type="file" class="file" id="photo_upload" name="photo_upload" accept="image/*"> …Run Code Online (Sandbox Code Playgroud) 在我的服务器端,我正在使用带有信号器的web api 2.在我的客户端,我正在使用angularjs.
当我启动信号器连接时,这是http请求:
> GET
> http://example.com/signalr/negotiate?clientProtocol=1.4&connectionData=%5B%7B%22name%22%3A%22main%22%7D%5D&_=1416702959615 HTTP/1.1 Host: mysite.net Connection: keep-alive Pragma: no-cache
> Cache-Control: no-cache Accept: text/plain, */*; q=0.01 Origin:
> http://lh:51408 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64)
> AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65
> Safari/537.36 Content-Type: application/x-www-form-urlencoded;
> charset=UTF-8 Referer: http://localhost:51408/ Accept-Encoding: gzip,
> deflate, sdch Accept-Language: en-US,en;q=0.8 Cookie:
> ARRAffinity=9def17406de898acdc2839d0ec294473084bbc94a8f600c867975ede6f136080
Run Code Online (Sandbox Code Playgroud)
并回应:
> HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache
> Transfer-Encoding: chunked Content-Type: application/json;
> charset=UTF-8 Expires: -1 Server: Microsoft-IIS/8.0
> X-Content-Type-Options: nosniff X-AspNet-Version: 4.0.30319 …Run Code Online (Sandbox Code Playgroud) 现在我正在尝试创建一个在我的存储库中包含外键的通用方法.
我现在得到的是:
public static class ExtensionMethods
{
private static IQueryable<T> IncludeProperties<T>(this DbSet<T> set, params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> queryable = set;
foreach (var includeProperty in includeProperties)
{
queryable = queryable.Include(includeProperty);
}
return queryable;
}
}
Run Code Online (Sandbox Code Playgroud)
但是在编译时我收到错误:
类型'T'必须是引用类型才能在泛型类型或方法'System.Data.Entity.DbSet'中将其用作参数'TEntity'
这可能是什么问题?
在我的应用程序中,我为每个调用创建一个dbcontext,依赖项通过Ninject注入它.
我一直在考虑创建一个单例类(ContextManager - BaseController将在每个请求上设置上下文),以使上下文可用,从而允许所有服务共享相同的上下文.这将使得例如禁用代理创建等变得容易,因为仅从一个地方管理上下文.
但是,当对象是单个对象时,每个请求都会覆盖上下文,这对我来说不起作用(我不希望多个请求共享一个上下文).
最好的方法是什么(如何在请求范围内优选单个上下文)?
我正在尝试在我的对象上实例化每个属性(类型为JobVmInput).到目前为止这是我的代码:
var properties = this.GetType().GetProperties();
foreach (var p in properties)
{
var type = p.PropertyType;
if (type.IsSubclassOf(typeof(JobVmInput)))
{
var constructor = type.cons.GetConstructor(**what to input here**);
p.SetValue(p, constructor.Invoke(**what to input here**));
}
}
Run Code Online (Sandbox Code Playgroud)
但是我不知道在GetConstructor和constructor.Invoke方法中输入什么.我查看了MSDN文档,但我对他们接受的内容非常不确定.我只想调用空的默认构造函数.
我希望能够使用强类型语法检索类型属性的名称.我已经有一个函数来获取实例的属性名称:
public static string PropertyName<T, TReturn>(this T obj, Expression<Func<T, TReturn>> property) where T : class
{
MemberExpression body = (MemberExpression) property.Body;
if (body == null) throw new ArgumentException("The provided expression did not point to a property.");
return body.Member.Name;
}
Run Code Online (Sandbox Code Playgroud)
可以这样调用:
Car car = new Car();
car.PropertyName(x => x.Wheels) //returns "Wheels"
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建另一个可以支持以下功能:
Type t = Typeof(Car);
t.PropertyName(x => x.Wheels) //should return "Wheels"
Run Code Online (Sandbox Code Playgroud)
或者只是(甚至更好!):
Car.PropertyName(x => x.Wheels)
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我的项目的结构如下:
// Abstract class
public abstract class Job
{
public abstract JobResult Run();
}
public abstract class JobResult { }
// Concrete implementer
public class Job1 : Job
{
public override Job1Result Run() { }
}
public class Job1Result : JobResult { }
Run Code Online (Sandbox Code Playgroud)
每个具体作业都继承自 Job 并实现 Run 方法,该方法返回 JobResult 的具体类。
但是,当我这样做时,我收到编译器错误:
Job1.Run()':返回类型必须是 JobResult 以匹配重写的成员 Job.Run()
重写抽象方法时真的不能返回返回类型的继承对象吗?
在我的ASP.NET MVC应用程序中,我将运行后台任务,有时持续约10分钟.我已经阅读了这个主题,看到应用程序域有时会重新启动,从而删除我的线程.
所以我找了一些解决方案.我找到的主要解决方案是Hangfire.我知道它会在失败等情况下重新启动作业,但是他们会以某种方式"绕过"应用程序域重新启动,还是只在这种情况下重新启动作业?
c# ×8
asp.net ×4
.net ×2
angularjs ×2
cors ×2
database ×2
reflection ×2
api ×1
appdomain ×1
asp.net-mvc ×1
azure ×1
constructor ×1
enums ×1
generics ×1
ghostdriver ×1
hangfire ×1
inheritance ×1
phantomjs ×1
signalr ×1
web ×1