我刚刚爱上了NHibernate和流畅的界面.后者支持非常好的映射和重构支持(不再需要xml文件).
但没有人是完美的,所以我错过了流利的多对多映射.有人知道它是否已经存在吗?如果是这样,一行简单的代码就会很好.
但要坚持问题的标题,有没有办法结合流畅和正常的NHibernate映射.
目前我使用以下行进行我的测试设置WITH Fluent,以及第二个代码块用于我的测试WITHOUT流畅(使用XML映射).如何判断流利使用流畅的IF AVAILABLE和XML否则......
var cfg = new Configuration();
cfg.AddProperties(MsSqlConfiguration.MsSql2005.ConnectionString.Is(_testConnectionstring).ToProperties());
cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly);
new SchemaExport(cfg).Create(true, true);
var persistenceModel = new PersistenceModel();
persistenceModel.addMappingsFromAssembly(typeof(CatMap).Assembly);
IDictionary<string, string> properties = MsSqlConfiguration.MsSql2005.UseOuterJoin().ShowSql().ConnectionString.Is(_testConnectionstring).ToProperties();
properties.Add("command_timeout", "340");
session = new SessionSource(properties, persistenceModel).CreateSession();
Run Code Online (Sandbox Code Playgroud)
没有流利......
config = new Configuration();
IDictionary props = new Hashtable();
props["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
props["dialect"] = "NHibernate.Dialect.MsSql2005Dialect";
props["connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
props["connection.connection_string"] = "Server=localhost;initial catalog=Debug;Integrated Security=SSPI";
props["show_sql"] = "true";
foreach (DictionaryEntry de in props)
{
config.SetProperty(de.Key.ToString(), de.Value.ToString());
}
config.AddAssembly(typeof(CatMap).Assembly);
SchemaExport se = new SchemaExport(config);
se.Create(true, true);
factory = …Run Code Online (Sandbox Code Playgroud) 我想生成一个数据库脚本,而没有声明实际的数据库连接字符串.
为了做到这一点,我现在使用NHibernate ExportSchema基于用Fluent NHibernate生成的NHibernate配置(在我的ISessionFactory创建方法期间):
FluentConfiguration configuration = Fluently.Configure();
//Mapping conf ...
configuration.Database(fluentDatabaseProvider);
this.nhibernateConfiguration = configuration.BuildConfiguration();
returnSF = configuration.BuildSessionFactory();
//Later
new SchemaExport(this.nhibernateConfiguration)
.SetOutputFile(filePath)
.Execute(false, false, false);
Run Code Online (Sandbox Code Playgroud)
fluentDatabaseProvider是一个FluentNHibernate IPersistenceConfigurer,需要获取正确的sql方言来创建数据库.
使用现有数据库创建工厂时,一切正常.但我想要做的是在选定的数据库引擎上创建一个NHibernate配置对象,而不需要在场景后面有真正的数据库...而且我无法做到这一点.
如果有人有一些想法.
我目前正在学习使用Propel ORM,我想重用一个critera用于两个稍微不同的查询:
$criteria = ArticleQuery::create()
->filterByIsPublished(true)
->orderByPublishFrom(Criteria::DESC)
->joinWith('Article.Author')
->keepQuery();
$this->news = $criteria
->filterByType('news')
->find();
$this->articles = $critera
->filterByType('article')
->find();
Run Code Online (Sandbox Code Playgroud)
但是,这不会按预期工作,因为现在对文章的查询将尝试查找类型为"新闻"和"文章"的条目,这当然是不可能的.
所以我们需要得到这个对象的克隆,对我来说似乎直观的是简单地在paranthesis中添加clone关键字:
$this->news = (clone $criteria)
->filterByType('news')
->find();
Run Code Online (Sandbox Code Playgroud)
Parse error: syntax error, unexpected T_OBJECT_OPERATOR
相反,我们必须先将它分配给变量才能使用它:
$clonedCritera = clone $criteria;
$this->news = $clonedCriteria
->filterByType('news')
->find();
Run Code Online (Sandbox Code Playgroud)
您与new运营商具有相同的行为.我看到推进开发者通过替换:
new ArticleQuery()->doOperations()with来规避这个限制ArticleQuery::create()->doOperations().
为什么PHP语言设计者选择这样做?如果你可以直接使用这些表达式的结果,它将使代码更流畅,在某些情况下,更容易阅读.
我有两张桌子 -
1. Account
2. Users
Run Code Online (Sandbox Code Playgroud)
在Account表中,DefaultExpensePartner并AccountOwner有外键UserId的字段Users的表.我已经定义了类如下.
public class Account
{
public int AccountId { get; set; }
public string AccountName { get; set; }
public int? AccountOwnerId { get; set; }
public int? DefaultExpensePartnerId { get; set; }
public virtual Users AccountOwner { get; set; }
public virtual Users DefaultExpensePartner { get; set; }
}
public class AccountConfiguration : EntityTypeConfiguration<Account>
{
public AccountConfiguration()
{
this.ToTable("Account");
this.HasKey(c => c.AccountId);
this.Property(c …Run Code Online (Sandbox Code Playgroud) 我正在使用Fluent NHibernate映射现有数据库,并且在尝试填充多对多集合时遇到了问题.数据库本身没有使用外键正确设置,这是我遇到的问题的简化示例.
表:
public class User
{
public virtual long UserID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Group> Groups { get; set; }
}
public class Group
{
public virtual long GroupID { get; set; }
public virtual string Name { get; set; }
public virtual IList<User> Users { get; set; }
}
public class UserInGroup
{
public virtual User User { get; set; }
public virtual Group …Run Code Online (Sandbox Code Playgroud) c# nhibernate fluent fluent-nhibernate fluent-nhibernate-mapping
这是交易.我用两个或多或少的表来设置我的数据库(实际上,我的表格要复杂得多,但这是要点):
表`元素`
表`element_drafts`
正如您可能从名称中猜到的那样,第二个表包含第一个表中的行草稿行.(不要担心建议更好的方法来做草稿=)我的表比显示的更复杂,并且有一个草稿表是最好的解决方案atm).
我在两个表之间建立了一个关系,这样我就可以做到这样的事情:
// Get all of the elements along with their draft rows
$elements_with_drafts = Element::with('drafts')->all();
Run Code Online (Sandbox Code Playgroud)
我也可以通过这样做只选择有草稿行的元素:
$elements_with_drafts = Element::with('drafts')
->whereIn('id', function($query)
{
$query->select('element_id')->from('element_drafts');
})
->get();
Run Code Online (Sandbox Code Playgroud)
但是我想做的一件事我无法弄清楚:将草稿值复制到其父元素.
我不完全确定这是可能的.是吗?
我的猜测是,它可能会这样做:
$elements_with_drafts = Element::with('drafts')
->whereIn('id', function($query)
{
$query->select('element_id')->from('element_drafts');
})
->update(array("data" => function($query)
{
// Somehow select the draft value?
});
Run Code Online (Sandbox Code Playgroud)
我觉得我很亲密,但我不太确定如何做我想做的事.有任何想法吗?
注意:显然这可以通过foreach循环相对容易地完成,但我希望解决方案只是一个查询.
我有这个代码的问题:
RuleFor(field => field.TermEndDate)
.NotEmpty()
.When(x => x.TermEndDate == x.TermStartDate)
.WithMessage("error...");
Run Code Online (Sandbox Code Playgroud)
我设置TermEndDate = DateTime.Now但没有消息加注!
我的测试代码是:
var now = DateTime.Now;
var command = new AddTermCommand
{
SchoolId = Guid.NewGuid(),
TermStartDate = now,
TermEndDate = now
};
var cmd = command.Validate();
if (!cmd.IsValid)
Console.WriteLine(cmd.Errors.First().ErrorMessage);
Run Code Online (Sandbox Code Playgroud) 我知道有类似的问题.我没有看到我的问题的答案.
我会用一些简化的代码呈现我想要的东西.假设我有一个复杂的对象,它的一些值是通用的:
public static class SomeObject<T, S> {
public int number;
public T singleGeneric;
public List<S> listGeneric;
public SomeObject(int number, T singleGeneric, List<S> listGeneric) {
this.number = number;
this.singleGeneric = singleGeneric;
this.listGeneric = listGeneric;
}
}
Run Code Online (Sandbox Code Playgroud)
我想用流畅的Builder语法构造它.我想让它变得优雅.我希望它能像那样工作:
SomeObject<String, Integer> works = new Builder() // not generic yet!
.withNumber(4)
// and only here we get "lifted";
// since now it's set on the Integer type for the list
.withList(new ArrayList<Integer>())
// and the decision to go with String type for the …Run Code Online (Sandbox Code Playgroud) 我喜欢python.然而,有一点让我感到困惑的是,我不知道如何以流畅的方式格式化功能活动,如javascript中的can.
示例(当场随机创建):你能帮助我以流畅的方式将其转换为python吗?
var even_set = [1,2,3,4,5]
.filter(function(x){return x%2 === 0;})
.map(function(x){
console.log(x); // prints it for fun
return x;
})
.reduce(function(num_set, val) {
num_set[val] = true;
}, {});
Run Code Online (Sandbox Code Playgroud)
我想知道是否有流体选择?也许是图书馆.
一般来说,我一直在使用列表推理来处理大多数事情,但如果我想要打印,这是一个真正的问题
例如,我如何使用列表理解(Python 3 print()作为函数打印python 2.x中的1到5之间的每个偶数,但Python 2则不打印).构建并返回列表也有点烦人.我宁愿只是为了循环.