我认为这在nhiberate中是可能的,但我的问题是关于实体框架.
在我的数据库模型中 - 我无法修改 - 我有冗余列,我想存储在不同的类中.
示例:
public class DateParams
{
public DateTime CreationDate { get; set; }
public DateTime ModificationDate { get; set; }
// some methods
}
public class Localization
{
public String EnglishLabel { get; set; }
public String FrenchLabel { get; set; }
// some methods
}
Run Code Online (Sandbox Code Playgroud)
然后我会在我的一些模型中使用它们:
public class Account // Localization && DateParams
{
public int ID { get; set; }
public String Name { get; set; }
public Localization Localization { get; set; …Run Code Online (Sandbox Code Playgroud) 在EF Core和EF6中,调用属性'Date'的getter(请参见下文)将提供正确的值,但是请注意两者之间的细微差别:在EF Core中,永不调用setter!
这是我的模型:
public class MyModel
{
private DateTime _Date;
private bool dateTimeHasBeenSet = false;
public DateTime Date
{
get
{
return _Date;
}
set
{
dateTimeHasBeenSet = true;
_Date = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我检索单个项目的方式:
//Entity Framework 6
using (Ef6Context context = new Ef6Context())
{
var m = context.MyModels.First();
// m.dateTimeHasBeenSet is true
}
//Entity Framework Core
using (EfCoreContext context = new EfCoreContext())
{
var m = context.MyModels.First();
// m.dateTimeHasBeenSet is false
}
Run Code Online (Sandbox Code Playgroud)
EF核心是否初始化备份字段而不是属性(通过反射)?这不是违反封装吗?
我正在将一些代码从EF6迁移到EF Core,我真的想避免浪费时间手动调用每个setter背后的逻辑...
编辑:问这个问题让我尝试了一些幻想,如果我将_Property的备份字段重命名为_PropertyX之类的东西(在本例中为_DateX),我的设置器就会被EF …
EDIT3:好的,我发现了问题:如果应用程序被杀死,则同时发送data 和 发送都notification不会触发onMessageReceived。notification如果目标设备是android,则需要将其设置为null。
这确实是一种愚蠢的行为。
原始帖子:
notificationBuilder启动应用程序后,我可以成功地从传递给活动的意图中检索捆绑包。
但是,如果我终止了该应用程序,则通知仍然有效,但GetExtras()意图上的则为null。
在我的FirebaseMessagingService孩子班上:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("key_1","value");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//also tried with PendingIntent.FLAG_CANCEL_CURRENT
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("messageBody")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
当应用被杀死时,firebaseMessagingService甚至无法写入文件。它不会崩溃,它会生成并显示通知,但是对磁盘所做的任何操作均不起作用。
编辑:我已替换onMessageReceived方法的内容,如下所示:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{ …Run Code Online (Sandbox Code Playgroud) 我遇到了Linq的一个奇怪的行为:有两个linq表达式可能看起来相同,我有不同的结果!如果我循环一次我得到相同的结果,但在上面它什么都没发现.
这是代码:
Dictionary<String, String> mainDico = new Dictionary<String, String>();
mainDico.Add("key1", "value1");
mainDico.Add("key2", "value2");
List<Dictionary<String, String>> ls = new List<Dictionary<String, String>>();
Dictionary<String, String> fistDico = new Dictionary<String, String>();
fistDico.Add("key1", "value1");
fistDico.Add("key2", "value2");
Dictionary<String, String> secondDico = new Dictionary<String, String>();
secondDico.Add("key1", "other");
secondDico.Add("key2", "other");
ls.Add(fistDico);
ls.Add(secondDico);
IEnumerable<Dictionary<String, String>> failQuery = from p in ls
select p;
IEnumerable<Dictionary<String, String>> successfulQuery = from p in ls
select p;
String[] items = new String[] { "key1","key2" }; // with one element it works
foreach (var …Run Code Online (Sandbox Code Playgroud) EF6: inserting an entity that already has a value for the primary key works and assigns a new value to the primary key.
EF Core : it tries to insert the value of the primary key and obviously fails with a SqlException:
Cannot insert explicit value for identity column in table 'Asset' when IDENTITY_INSERT is set to OFF.
The workaround I found is to reset the PK to the default value (0 for an int).
Example :
Asset asset …Run Code Online (Sandbox Code Playgroud) 我的实体框架上下文:
public class MyContext : DbContext
{
public DbSet<Company> Companies { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Event> Events { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我添加对象的方法:
public void AddObject(DbContext context, Type t, object object_to_add)
{
PropertyInfo context_property =
context.GetType().GetProperties()
.Where(p => typeof(IEnumerable).IsAssignableFrom(p.PropertyType)
&& p.PropertyType.GenericTypeArguments.Any()
&& p.PropertyType.GenericTypeArguments.First() == t)
.Single();
DbSet db_set = (DbSet)context_property.GetMethod.Invoke(context, null);
db_set.Add(object_to_add);
}
Run Code Online (Sandbox Code Playgroud)
但是,代码崩溃当我尝试投了DbSet<>到DbSet,
我使用反射是因为我收到映射到现有模型的DTO对象(也通过反射),我不想为每个新模型编写add方法(我喜欢40,并且列表呈指数增长)
块引用
有什么办法吗?
我有一位同事以这种方式使用三元运算符(在javascript中):
var genderLabel = '';
isMale? genderLabel = 'Man' : genderLabel = 'Woman';
Run Code Online (Sandbox Code Playgroud)
在 C# 中,我就这样做。
var genderLabel = isMale? "Man" : "Woman";
Run Code Online (Sandbox Code Playgroud)
我的同事说这是 javascript 编码约定...是真的吗?我不是 javascript 专家,我不喜欢这种语言......当我检查代码时,我专注于我的左手边以遵循变量初始化或赋值,这种风格迫使我阅读整行。
我还在维护一位前雇员的Java代码,他以同样的方式使用三元运算符。这是一种反模式吗?我认为编译器应该禁止它,就像在 if 语句中禁止它一样:
if(x = 2)
{
...
}
Run Code Online (Sandbox Code Playgroud)
这不会在 C# 中编译。
c# ×5
android ×1
coding-style ×1
dictionary ×1
firebase ×1
javascript ×1
linq ×1
reflection ×1