小编Jon*_*han的帖子

DatabaseGeneratedOption.Identity不生成Id

首先使用EntityFramework代码,我创建了一个简单的Foo表.这是我的实体:

public class Foo
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual string Id { get; set; }

    public virtual string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但是每当我尝试插入一个新行时,我都会得到一个Cannot insert the value NULL into column 'Id'.当我添加DatabaseGenerated属性时,为什么会发生这种情况?删除和重新创建我的表没有区别.

c# asp.net-mvc entity-framework

9
推荐指数
2
解决办法
2万
查看次数

在空模型的情况下返回错误响应

在我的ASP.NET MVC API应用程序中,ErrorResponse如果我的一些Required字段丢失,我可以返回一个有用的内容:

return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
Run Code Online (Sandbox Code Playgroud)

-

"Message": "The request is invalid.",
        "ModelState": {
            "myModel.FooA": [
                "The FooA is required."
            ],
            "myModel.FooC": [
                "The FooC property is required."
            ],
            "myModel.FooD": [
                "The FooD property is required."
            ]
        }
Run Code Online (Sandbox Code Playgroud)

但是,正如这个答案所证实的那样,NULL模型将会验证.由于我不允许这样做,我如何返回一个同样有用的错误响应,说明所需的所有值?我知道我可以为每个属性手动添加ModelError,但我怀疑可能有一种方法CreateErrorResponse可以为我做这个.

asp.net-mvc asp.net-mvc-3

8
推荐指数
1
解决办法
537
查看次数

Doctrine中的分离实体错误

我将一组实体发布到控制器,我想删除所有这些实体.但是,以下代码会引发A detached entity was found during removed MyProject\Bundle\MyBundle\Entity\MyEntity@000000004249c13f00000001720a4b59错误.我哪里错了?

$doctrineManager = $this->getDoctrine()->getManager();
foreach ($form->getData()->getEntities() as $entity) {
    $doctrineManager->merge($entity);  
    $doctrineManager->remove($entity);
}
$doctrineManager->flush();
Run Code Online (Sandbox Code Playgroud)

php doctrine symfony

8
推荐指数
1
解决办法
1万
查看次数

在循环中执行Doctrine查询时内存泄漏

我在查找脚本中的内存泄漏原因时遇到了麻烦.我有一个简单的存储库方法,它将我的实体中的'count'列递增X量:

public function incrementCount($id, $amount)
{
    $query = $this
        ->createQueryBuilder('e')
        ->update('MyEntity', 'e')
        ->set('e.count', 'e.count + :amount')
        ->where('e.id = :id')
        ->setParameter('id', $id)
        ->setParameter('amount', $amount)
        ->getQuery();

    $query->execute();
}
Run Code Online (Sandbox Code Playgroud)

问题是,如果我在循环中调用它,则每次迭代都会占用内存使用量:

$doctrineManager = $this->getContainer()->get('doctrine')->getManager();
$myRepository = $doctrineManager->getRepository('MyEntity');
while (true) {
    $myRepository->incrementCount("123", 5);
    $doctrineManager->clear();
    gc_collect_cycles();
}
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?->clear()根据Doctrine 关于批处理建议,我试过了.我甚至尝试过gc_collect_cycles(),但问题仍然存在.

我在PHP 5.5上运行Doctrine 2.4.6.

php memory-leaks doctrine symfony doctrine-orm

8
推荐指数
5
解决办法
6957
查看次数

如何避免代码重复

我有三个C#中的方法几乎执行相同.更具体地说,存在大量的代码重复,但最后,它们执行不同的功能.

这显然非常低效,那么减少代码重复的最佳方法是什么?我应该将它全部放在一个方法中,并在最后使用switch和enum来实现不同的功能吗?或者,是否有某种方式可以有3个单独的类并从另一个继承共享位?我已经对此进行了相当多的阅读,从我可以收集到的内容来看,这仅用于获取来自不同类别的属性.

c# code-reuse

7
推荐指数
1
解决办法
1147
查看次数

在MVC 3中使用带有IEnumerable <string>的EditorFor

我有一个包含字符串的IEnumerable,使用Data Annotations进行验证:

[Required(ErrorMessage = "This is required.")]
[Remote("IsValid", "ControllerName")]
public IEnumerable<string> MyList { get; set; }    
Run Code Online (Sandbox Code Playgroud)

我正在使用它与编辑器模板.这是我在视图中称之为的方式:

@Html.EditorFor(m => m.MyList)
Run Code Online (Sandbox Code Playgroud)

最后,我的模板采用了这个IEnumarable并为每个元素创建了许多表单元素:

@model IEnumerable<string>
@foreach (var str in Model)
{
    <li>
        @Html.LabelFor(m => str, "My Label")
        @Html.TextBoxFor(m => str)
        @Html.ValidationMessageFor(m => str)
    </li>
}
Run Code Online (Sandbox Code Playgroud)

即使表单元素确实正确呈现,我是否正确接近?此外,我注意到它不再验证.我该如何解决这个问题?

asp.net-mvc razor

7
推荐指数
1
解决办法
4027
查看次数

如何使用自定义消息 git stash 未跟踪的文件?

如何git stash --include-untracked使用自定义消息执行操作?

我试过了:

git stash --include-untracked --message "Foo"

git stash "Foo" --include-untracked

但两者都会在 Git 2.17.1 中打印使用警告。

git

7
推荐指数
1
解决办法
2475
查看次数

CABasicAnimation - 设置起始行程位置

我正在绘制一个基本圆圈的动画.这很好用,除了动画开始在3点位置绘制.有谁知道我怎么能在12点开始?

self.circle = [CAShapeLayer layer];
self.circle.fillColor = nil;
self.circle.lineWidth = 7;
self.circle.strokeColor = [UIColor blackColor].CGColor;
self.circle.bounds = CGRectMake(0, 0, 200, 200);
self.circle.path = [UIBezierPath bezierPathWithOvalInRect:self.circle.bounds].CGPath;
[self.view.layer addSublayer:self.circle];

CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration            = 5.0;
drawAnimation.repeatCount         = 1.0;
drawAnimation.removedOnCompletion = NO;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
Run Code Online (Sandbox Code Playgroud)

objective-c cabasicanimation cashapelayer ios

6
推荐指数
1
解决办法
3030
查看次数

在boto3中分页DynamoDB查询

如果它们跨越多个页面,我如何遍历DynamoDB查询中的所有结果?这个答案意味着分页内置于查询函数中(至少在v2中),但是当我在v3中尝试这个时,我的项目似乎有限:

import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource('dynamodb')
fooTable = dynamodb.Table('Foo')
response = fooTable.query(
    KeyConditionExpression=Key('list_id').eq('123')
)

count = 0

for i in response['Items']:
    count += 1

print count # Prints a subset of my total items
Run Code Online (Sandbox Code Playgroud)

python pagination boto3 aws-lambda

6
推荐指数
1
解决办法
2369
查看次数

从 ASP.NET Identity 中删除“AspNetUserLogins”表

由于我不会使用任何其他登录提供程序,因此我不需要该AspNetUserLogins表。我有什么办法可以将其删除吗?

我试过这个:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Ignore<IdentityUserLogin<string>>();
}
Run Code Online (Sandbox Code Playgroud)

Invalid object name 'dbo.UserLogin'每当我打电话时我都会收到错误UserManager.FindAsync(username, password)

提前致谢。

c# sql asp.net-mvc asp.net-identity

5
推荐指数
1
解决办法
3148
查看次数