Entity Framework Type for Storing Only Year and Month
I want to save production year and month on an entity. I don't want to save the time and date of the month.
I can come up with multiple solutions, and the best two so far is:
public class Product
{
private DateTime _productionMonth;
public DateTime ProductionMonth
{
get => _productionMonth;
set => _productionMonth = new DateTime(value.Year, value.Month, 1);
}
}
or
public class Product
{
public int Year { get; set; }
public int Month { get; set; }
}
But I don't feel any of the solutions is the perfect solution...
Does there exists any better solutions? Maybe an attribute on DateTime
?
c# entity-framework tsql datetime
add a comment |
I want to save production year and month on an entity. I don't want to save the time and date of the month.
I can come up with multiple solutions, and the best two so far is:
public class Product
{
private DateTime _productionMonth;
public DateTime ProductionMonth
{
get => _productionMonth;
set => _productionMonth = new DateTime(value.Year, value.Month, 1);
}
}
or
public class Product
{
public int Year { get; set; }
public int Month { get; set; }
}
But I don't feel any of the solutions is the perfect solution...
Does there exists any better solutions? Maybe an attribute on DateTime
?
c# entity-framework tsql datetime
How about storing them in two shortint fields?
– M. Mennan Kara
Nov 22 '18 at 10:19
@M.MennanKara It's a better solution thanint
. But saving them in seperate fields makes it complicated to search for a range.
– Jogge
Nov 22 '18 at 10:22
1
How about something like year * 12 + month in an int field?
– M. Mennan Kara
Nov 22 '18 at 10:27
add a comment |
I want to save production year and month on an entity. I don't want to save the time and date of the month.
I can come up with multiple solutions, and the best two so far is:
public class Product
{
private DateTime _productionMonth;
public DateTime ProductionMonth
{
get => _productionMonth;
set => _productionMonth = new DateTime(value.Year, value.Month, 1);
}
}
or
public class Product
{
public int Year { get; set; }
public int Month { get; set; }
}
But I don't feel any of the solutions is the perfect solution...
Does there exists any better solutions? Maybe an attribute on DateTime
?
c# entity-framework tsql datetime
I want to save production year and month on an entity. I don't want to save the time and date of the month.
I can come up with multiple solutions, and the best two so far is:
public class Product
{
private DateTime _productionMonth;
public DateTime ProductionMonth
{
get => _productionMonth;
set => _productionMonth = new DateTime(value.Year, value.Month, 1);
}
}
or
public class Product
{
public int Year { get; set; }
public int Month { get; set; }
}
But I don't feel any of the solutions is the perfect solution...
Does there exists any better solutions? Maybe an attribute on DateTime
?
c# entity-framework tsql datetime
c# entity-framework tsql datetime
edited Nov 22 '18 at 12:53
Jogge
asked Nov 22 '18 at 10:16
JoggeJogge
419215
419215
How about storing them in two shortint fields?
– M. Mennan Kara
Nov 22 '18 at 10:19
@M.MennanKara It's a better solution thanint
. But saving them in seperate fields makes it complicated to search for a range.
– Jogge
Nov 22 '18 at 10:22
1
How about something like year * 12 + month in an int field?
– M. Mennan Kara
Nov 22 '18 at 10:27
add a comment |
How about storing them in two shortint fields?
– M. Mennan Kara
Nov 22 '18 at 10:19
@M.MennanKara It's a better solution thanint
. But saving them in seperate fields makes it complicated to search for a range.
– Jogge
Nov 22 '18 at 10:22
1
How about something like year * 12 + month in an int field?
– M. Mennan Kara
Nov 22 '18 at 10:27
How about storing them in two shortint fields?
– M. Mennan Kara
Nov 22 '18 at 10:19
How about storing them in two shortint fields?
– M. Mennan Kara
Nov 22 '18 at 10:19
@M.MennanKara It's a better solution than
int
. But saving them in seperate fields makes it complicated to search for a range.– Jogge
Nov 22 '18 at 10:22
@M.MennanKara It's a better solution than
int
. But saving them in seperate fields makes it complicated to search for a range.– Jogge
Nov 22 '18 at 10:22
1
1
How about something like year * 12 + month in an int field?
– M. Mennan Kara
Nov 22 '18 at 10:27
How about something like year * 12 + month in an int field?
– M. Mennan Kara
Nov 22 '18 at 10:27
add a comment |
1 Answer
1
active
oldest
votes
You could store it as a single integer, using YYYYMM format. The entity can expose unmapped ([NotMapped]
) helper methods to covering to/from DateTime
, and/or a ValueTuple (year int, month int)
though from EF queries you would need to use the single mapped value. YYYYMM are sortable so you can query a range efficiently.
Great idea!
– Jogge
Nov 22 '18 at 10:36
You could, but then you would have to write validators to make sure the month is between 1 and 12 (inclusive), and the year is reasonable (since it's an int, nothing is stopping you from storing a value like2001815
(that's year 20018 month 15 - obviously wrong) .
– Zohar Peled
Nov 22 '18 at 11:09
Apply DDD. Private/Internal setter on the entity, only accept setting via a method/property that accepts a DateTime. Constructor/factory accepts a DateTime for the value. You would arguably have validators on a DateTime in any case for validity.
– Steve Py
Nov 22 '18 at 21:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428640%2fentity-framework-type-for-storing-only-year-and-month%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could store it as a single integer, using YYYYMM format. The entity can expose unmapped ([NotMapped]
) helper methods to covering to/from DateTime
, and/or a ValueTuple (year int, month int)
though from EF queries you would need to use the single mapped value. YYYYMM are sortable so you can query a range efficiently.
Great idea!
– Jogge
Nov 22 '18 at 10:36
You could, but then you would have to write validators to make sure the month is between 1 and 12 (inclusive), and the year is reasonable (since it's an int, nothing is stopping you from storing a value like2001815
(that's year 20018 month 15 - obviously wrong) .
– Zohar Peled
Nov 22 '18 at 11:09
Apply DDD. Private/Internal setter on the entity, only accept setting via a method/property that accepts a DateTime. Constructor/factory accepts a DateTime for the value. You would arguably have validators on a DateTime in any case for validity.
– Steve Py
Nov 22 '18 at 21:16
add a comment |
You could store it as a single integer, using YYYYMM format. The entity can expose unmapped ([NotMapped]
) helper methods to covering to/from DateTime
, and/or a ValueTuple (year int, month int)
though from EF queries you would need to use the single mapped value. YYYYMM are sortable so you can query a range efficiently.
Great idea!
– Jogge
Nov 22 '18 at 10:36
You could, but then you would have to write validators to make sure the month is between 1 and 12 (inclusive), and the year is reasonable (since it's an int, nothing is stopping you from storing a value like2001815
(that's year 20018 month 15 - obviously wrong) .
– Zohar Peled
Nov 22 '18 at 11:09
Apply DDD. Private/Internal setter on the entity, only accept setting via a method/property that accepts a DateTime. Constructor/factory accepts a DateTime for the value. You would arguably have validators on a DateTime in any case for validity.
– Steve Py
Nov 22 '18 at 21:16
add a comment |
You could store it as a single integer, using YYYYMM format. The entity can expose unmapped ([NotMapped]
) helper methods to covering to/from DateTime
, and/or a ValueTuple (year int, month int)
though from EF queries you would need to use the single mapped value. YYYYMM are sortable so you can query a range efficiently.
You could store it as a single integer, using YYYYMM format. The entity can expose unmapped ([NotMapped]
) helper methods to covering to/from DateTime
, and/or a ValueTuple (year int, month int)
though from EF queries you would need to use the single mapped value. YYYYMM are sortable so you can query a range efficiently.
answered Nov 22 '18 at 10:27
Steve PySteve Py
5,61011017
5,61011017
Great idea!
– Jogge
Nov 22 '18 at 10:36
You could, but then you would have to write validators to make sure the month is between 1 and 12 (inclusive), and the year is reasonable (since it's an int, nothing is stopping you from storing a value like2001815
(that's year 20018 month 15 - obviously wrong) .
– Zohar Peled
Nov 22 '18 at 11:09
Apply DDD. Private/Internal setter on the entity, only accept setting via a method/property that accepts a DateTime. Constructor/factory accepts a DateTime for the value. You would arguably have validators on a DateTime in any case for validity.
– Steve Py
Nov 22 '18 at 21:16
add a comment |
Great idea!
– Jogge
Nov 22 '18 at 10:36
You could, but then you would have to write validators to make sure the month is between 1 and 12 (inclusive), and the year is reasonable (since it's an int, nothing is stopping you from storing a value like2001815
(that's year 20018 month 15 - obviously wrong) .
– Zohar Peled
Nov 22 '18 at 11:09
Apply DDD. Private/Internal setter on the entity, only accept setting via a method/property that accepts a DateTime. Constructor/factory accepts a DateTime for the value. You would arguably have validators on a DateTime in any case for validity.
– Steve Py
Nov 22 '18 at 21:16
Great idea!
– Jogge
Nov 22 '18 at 10:36
Great idea!
– Jogge
Nov 22 '18 at 10:36
You could, but then you would have to write validators to make sure the month is between 1 and 12 (inclusive), and the year is reasonable (since it's an int, nothing is stopping you from storing a value like
2001815
(that's year 20018 month 15 - obviously wrong) .– Zohar Peled
Nov 22 '18 at 11:09
You could, but then you would have to write validators to make sure the month is between 1 and 12 (inclusive), and the year is reasonable (since it's an int, nothing is stopping you from storing a value like
2001815
(that's year 20018 month 15 - obviously wrong) .– Zohar Peled
Nov 22 '18 at 11:09
Apply DDD. Private/Internal setter on the entity, only accept setting via a method/property that accepts a DateTime. Constructor/factory accepts a DateTime for the value. You would arguably have validators on a DateTime in any case for validity.
– Steve Py
Nov 22 '18 at 21:16
Apply DDD. Private/Internal setter on the entity, only accept setting via a method/property that accepts a DateTime. Constructor/factory accepts a DateTime for the value. You would arguably have validators on a DateTime in any case for validity.
– Steve Py
Nov 22 '18 at 21:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428640%2fentity-framework-type-for-storing-only-year-and-month%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How about storing them in two shortint fields?
– M. Mennan Kara
Nov 22 '18 at 10:19
@M.MennanKara It's a better solution than
int
. But saving them in seperate fields makes it complicated to search for a range.– Jogge
Nov 22 '18 at 10:22
1
How about something like year * 12 + month in an int field?
– M. Mennan Kara
Nov 22 '18 at 10:27